
正文
实现JFileChooser的多种文件类型限制(设置过滤器)
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
使用时直接调用方法。
// 多类型时使用
public void FileFilter(JFileChooser F) {
String[][] fileNames = { { ".java", "JAVA源程序 文件(*.java)" },
{ ".doc", "MS-Word 2003 文件(*.doc)" },
{ ".xls", "MS-Excel 2003 文件(*.xls)" } };
// 循环添加需要显示的文件
for (final String[] fileEName : fileNames) {
F.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File file) {
if (file.getName().endsWith(fileEName[0])
|| file.isDirectory()) {
return true;
}
return false;
} public String getDescription() {
return fileEName[1];
} });
}
}





