实现JFileChooser的多种文件类型限制(设置过滤器)

使用时直接调用方法。

 1 // 多类型时使用
 2     public void FileFilter(JFileChooser F) {
 3         String[][] fileNames = { { ".java", "JAVA源程序 文件(*.java)" },
 4                 { ".doc", "MS-Word 2003 文件(*.doc)" },
 5                 { ".xls", "MS-Excel 2003 文件(*.xls)" } };
 6         // 循环添加需要显示的文件
 7         for (final String[] fileEName : fileNames) {
 8             F.setFileFilter(new javax.swing.filechooser.FileFilter() {
 9                 public boolean accept(File file) {
10                     if (file.getName().endsWith(fileEName[0])
11                             || file.isDirectory()) {
12                         return true;
13                     }
14                     return false;
15                 }
16 
17                 public String getDescription() {
18                     return fileEName[1];
19                 }
20 
21             });
22         }
23     }
原文地址:https://www.cnblogs.com/hehaiyang/p/3733626.html