Swing中JFileChooser的用法
来源:互联网 发布:阿里云二级域名 端口 编辑:程序博客网 时间:2024/05/21 09:34
Swing中JFileChooser的用法
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没有提供默认的文件过滤器,但提供了过滤器的抽象超类,我们可以继承它。
i
复制代码
mport 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());
…
}
复制代码
- swing中JFileChooser的用法
- Swing中JFileChooser的用法
- Swing中关于JFileChooser的一些问题
- swing中JFileChooser
- java Swing JFileChooser 的使用
- JFileChooser的用法
- JFileChooser用法
- JFileChooser用法
- JFileChooser用法
- java swing 中使用JFileChooser读取Excel并进行解析
- 在SWING 的 JFileChooser如何俘获返回的事件
- JFileChooser (Java Swing提供的文件选择对话框)
- JFileChooser (Java Swing提供的文件选择对话框)
- Java Swing 使用JFileChooser保存带自定义后缀名的文件
- java中文件选择器JFileChooser的用法
- java中文件选择器JFileChooser的用法
- java中文件选择器JFileChooser的用法
- Swing中JRadioButton多选框的用法
- 2017年 Android第三方库
- 低功耗蓝牙BLE之广播
- Error: The SDK platform-tools version ((23)) is too old to check APIs compiled with API 23
- 4种Java引用浅解
- W10下,基于myeclipse的hadoop开发环境的配置
- Swing中JFileChooser的用法
- Quartz中时间表达式的设置-----corn表达式
- Clear Case 与Clear Quest的集成
- Linux下通过ftp命令下载windows下的文件
- 关于模式识别、机器学习、推荐系统、图像特征、数值计算、目标跟踪等方面个人主页及博客
- Java学习笔记之线程池
- python fabric 变量写到远端
- sts下使用maven 创建第一个springmvc
- PDF文件表格数据处理