JFileChooser使用详解

JFileChooser使用详解


2024年3月13日发(作者:)

JFileChooser 转载

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

转载

标签:

it

JFileChooser用来提供一个文件对话框,可以通过其showXxxDialog打开一

个模态对话框,或直接实例化并加入到其他组件。

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

//Create a file chooser

final JFileChooser fc = new JFileChooser();

. ..

//打开文件对话框

int returnVal = enDialog(aComponent);

//获取选择的文件

File file = ectedFile();

//打开保存对话框

int returnVal = veDialog();

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

下好处:

(1)chooser会记住当前文件夹。

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

可以修改对话框文件选择的模式,比如只允许选择文件 夹:

eSelectionMode(ORIES_ONLY);除此以外,还有

其他两个选项FILES_AND_DIRECTORIES和 FILES_ONLY。

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

2.1 为特别任务创建FileChooser

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

JFileChooser fc = new JFileChooser();

int returnVal = alog(, "Attach");

2.2 文件过滤器

默认情况下,Chooser显示所有检测到得文件(隐藏文件除外),JFileChooser

支持以下三类Filter,检查顺序依次:

(1)Built-in filtering:直接调用Chooser上方法设置此类过滤器,比如

是否显示隐藏文件,setFileHidingEnabled(false)。

(2)Application-controlled filtering:首先自定义一个FileFilter的

子类,通过调用setFileFilter方法设置该过滤器,此时对话框只显示该过滤器

允许的文件,该过滤器将会出现在对话框的过滤器列表上。

(3)User-choosable filtering:添加一些可选的过滤器,

osableFileFilter(new ImageFilter()),

默认情况下,可选过滤器包括“显示全部文件”过滤器,如想去掉该过滤项,使

用eptAllFileFilterUsed(false)。

2.3 自定文件视图(File view)

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

setFileView方法。

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

essory(new ImagePreview(fc));

1、基本用法

JFileChooser dlg = new JFileChooser();

logTitle("Open JPEG file");

int result = enDialog(this); // 打开"打开文件"对话框

// int result = veDialog(this); // 打"开保存文件"对话框

if (result == E_OPTION) {

File file = ectedFile();

...

}

2、自定义FileFilter

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

import lter;

public final class PictureFileFilter extends FileFilter {

private String extension;

private String description;

public PictureFileFilter(String extension, String description) {

super();

ion = extension;

ption = description;

}

public boolean accept(File f) {

if (f != null) {

if (ctory()) {

return true;

}

String extension = getExtension(f);

if (extension != null && IgnoreCase(ion)) {

return true;

}

}

return false;

}

public String getDescription() {

return description;

}

private String getExtension(File f) {

if (f != null) {

String filename = e();

int i = dexOf('.');

if (i > 0 && i < () - 1) {

return ing(i + 1).toLowerCase();

}

}

return null;

}

}

其实主要就是accept(File f)函数。上例中只有一个过滤器,多个过滤器可参

考JDK目录中“demojfcFileChooserDemosrc”中的

“”

3、多选

在基本用法中,设置

tiSelectionEnabled(true);

即可实现文件的多选。

读取选择的文件时需使用

File[] files = ectedFiles();

4、选择目录

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

其实,对话框有一个FileSelectionMode属性,其默认值为

“_ONLY”,只需要将其修改为

“ORIES_ONLY”即可。

JFileChooser c = new JFileChooser();

eSelectionMode(ORIES_ONLY);

logTitle("Select path to save");

int result = enDialog();

if (result == E_OPTION) {

String path = ectedFile().getAbsolutePath());

...

}


发布者:admin,转转请注明出处:http://www.yc00.com/web/1710339632a1740179.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信