`
天涯海角tour
  • 浏览: 170015 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

JFileChooser使用详解

    博客分类:
  • java
阅读更多

JFileChooser 转载

(2010-11-30 23:20:02)

转载

<script></script>标签:

it

 

  JFileChooser用来提供一个文件对话框,可以通过其showXxxDialog打开一个模态对话框,或直接实例化并加入到其他组件。

   1、直接通过API打开对话框

   //Create a file chooser
   final JFileChooser fc = new JFileChooser();
   . ..
   //
打开文件对话框
   int returnVal = fc.showOpenDialog(aComponent);

   //获取选择的文件

   File file = fc.getSelectedFile();

   //打开保存对话框

   int returnVal = fc.showSaveDialog(FileChooserDemo.this);

   注意:在程序中,使用同一个JFileChooser对象创建打开和保存对话框有以下好处:

  1chooser会记住当前文件夹。

  2)只需设置chooser一次,然后多次使用。

   可以修改对话框文件选择的模式,比如只允许选择文件  夹:fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);除此以外,还有其他两个选项FILES_AND_DIRECTORIES FILES_ONLY

 

   2、定制FileChooser(包括文件过滤器、文件图标、文件预览图)

   2.1 为特别任务创建FileChooser

   除打开,保存文件框以外,可能还需要其他一些特殊的文件框,使用:

   JFileChooser fc = new JFileChooser();
   int returnVal = fc.showDialog(FileChooserDemo2.this, "Attach");

   2.2 文件过滤器

   默认情况下,Chooser显示所有检测到得文件(隐藏文件除外),JFileChooser支持以下三类Filter,检查顺序依次:

   1Built-in filtering:直接调用Chooser上方法设置此类过滤器,比如是否显示隐藏文件,setFileHidingEnabled(false)

   2Application-controlled filtering:首先自定义一个FileFilter的子类,通过调用setFileFilter方法设置该过滤器,此时对话框只显示该过滤器允许的文件,该过滤器将会出现在对话框的过滤器列表上。

   3User-choosable filtering:添加一些可选的过滤器,fc.addChoosableFileFilter(new ImageFilter())

默认情况下,可选过滤器包括显示全部文件过滤器,如想去掉该过滤项,使用fc.setAcceptAllFileFilterUsed(false)

   2.3 自定文件视图(File view

   为了改变默认的文件视图,可以先自定义一个FileView子类,然后调用setFileView方法。

   2.4 自定附件组件(文件预览图)

   fc.setAccessory(new ImagePreview(fc));

 

 

 

 

 

 

1、基本用法

JFileChooser dlg = new JFileChooser();
dlg.setDialogTitle("Open JPEG file");
int result = dlg.showOpenDialog(this);  //
打开"打开文件"对话框
// int result = dlg.showSaveDialog(this);  //
"开保存文件"对话框
if (result == JFileChooser.APPROVE_OPTION) {
File file = dlg.getSelectedFile();
...
}

2、自定义FileFilter

JDK没有提供默认的文件过滤器,但提供了过滤器的抽象超类,我们可以继承它。

import javax.swing.filechooser.FileFilter;

public final class PictureFileFilter extends FileFilter {

private String extension;

private String description;

public PictureFileFilter(String extension, String description) {
super();
this.extension = extension;
this.description = description;
}

public boolean accept(File f) {
if (f != null) {
if (f.isDirectory()) {
return true;
}
String extension = getExtension(f);
if (extension != null && extension.equalsIgnoreCase(this.extension)) {
return true;
}
}
return false;
}

public String getDescription() {
return description;
}

private String getExtension(File f) {
if (f != null) {
String filename = f.getName();
int i = filename.lastIndexOf('.');
if (i > 0 && i < filename.length() - 1) {
return filename.substring(i + 1).toLowerCase();
}
}
return null;
}

}

其实主要就是accept(File f)函数。上例中只有一个过滤器,多个过滤器可参考JDK目录中“demo\jfc\FileChooserDemo\src”中的“ExampleFileFilter.java”


3
、多选

在基本用法中,设置

c.setMultiSelectionEnabled(true);

即可实现文件的多选。

读取选择的文件时需使用

File[] files = c.getSelectedFiles();

4、选择目录

利用这个打开对话框,不仅可以选择文件,还可以选择目录。

其实,对话框有一个FileSelectionMode属性,其默认值为“JFileChooser.FILES_ONLY”,只需要将其修改为“JFileChooser.DIRECTORIES_ONLY”即可。

JFileChooser c = new JFileChooser();
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
c.setDialogTitle("Select path to save");
int result = c.showOpenDialog(PrintDatetime.this);
if (result == JFileChooser.APPROVE_OPTION) {
String path = c.getSelectedFile().getAbsolutePath());
...
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics