【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“MyFileChooser.java”一文,供大家参考学习!
package com.dialog;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.util.*;
public class MyFileChooser {
JPanel jContentPane = new JPanel();
JFileChooser jfc = null;
String name = "";
String result = "";
String files = "";
private List listFile;
// 为对话框添加指定的过滤器(保存文件对话框)
public JFileChooser getSeveFileChooser() {
jfc = new JFileChooser() {
public String paramString() {
return "drhdrhdrh";
}
};
jfc.addChoosableFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".jpg")
|| f.isDirectory();
}
public String getDescription() {
return "jpg(*.jpg)";
}
});
jfc.addChoosableFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".bmp")
|| f.isDirectory();
}
public String getDescription() {
return "bmp(*.bmp)";
}
});
int i = jfc.showSaveDialog(jContentPane);
if (i == JFileChooser.APPROVE_OPTION) {
name = jfc.getCurrentDirectory() + "\\"
+ jfc.getSelectedFile().getName();
javax.swing.filechooser.FileFilter filefilter = jfc.getFileFilter();
String path = filefilter.getDescription();
if (path.equals("bmp(*.bmp)")) {
result = ".bmp";
} else {
result = ".jpg";
}
getPath();
}
return jfc;
}
public String getPath() {
return name + result;
}
public String getformat() {
if (result.equalsIgnoreCase(".bmp")) {
result = "bmp";
} else {
result = "jpg";
}
return result;
}
// 为对话框添加指定的过滤器(打开文件对话框)
public JFileChooser getOpenFileChooser() {
jfc = new JFileChooser() {
};
jfc.addChoosableFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".avi")||
f.getName().toLowerCase().endsWith(".mpg")
|| f.isDirectory();
}
public String getDescription() {
return "常见视频文件(*)";
}
});
jfc.addChoosableFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".mp3")||
f.getName().toLowerCase().endsWith(".aiff")||
f.getName().toLowerCase().endsWith(".mp2")||
f.getName().toLowerCase().endsWith(".mid")||
f.getName().toLowerCase().endsWith(".au")||
f.getName().toLowerCase().endsWith(".gsm")
|| f.isDirectory();
}
public String getDescription() {
return "常见音频文件(*)";
}
});
int i = jfc.showOpenDialog(jContentPane); //创建打开文件对话框
if (i == JFileChooser.APPROVE_OPTION) {
name = jfc.getSelectedFile().getPath();
files = jfc.getSelectedFile().getName();
dealFile();
fileName();
}
return jfc;
}
// 获取保存图片路径方法
public String dealFile() {
if (name != null) {
return name;
} else {
return null;
}
}
public String fileName() {
if (files != null) {
return files;
} else {
return null;
}
}
// 只获取磁盘文件夹方法
public JFileChooser getFolder() {
try {
jfc = new JFileChooser();
jfc.setFileSelectionMode(jfc.DIRECTORIES_ONLY); // 指示只显示目录
int n = jfc.showOpenDialog(jContentPane);
} catch (Exception ex) {
ex.printStackTrace();
}
return jfc;
}
public String getFolderPath() {
String filePath = jfc.getSelectedFile().getPath();
if(filePath != null){
return filePath;
}
else{
return null;
}
}
}