ExampleFileFilter需要自己编写,下面给出具体类代码的实现。 /** *Class <code> ExampleFileFilter </code> supply specify file type to be *selected. */ public class ExampleFileFilter extends javax.swing.filechooser.FileFilter { /** *The type of file can be accepted. */ public boolean accept(java.io.File f) { boolean accept = f.isDirectory(); if (!accept) { String suffix = getSuffix(f); if (suffix != null) accept = suffix.equals("desc"); } return accept; }
/** *Get the extends name of select file. */ public String getDescription() { return "Description Files (*.desc)"; }
/** *Get suffix of select file. */ private String getSuffix(java.io.File f) { String s = f.getPath(); String suffix = null; int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) suffix = s.substring(i + 1).toLowerCase(); return suffix; } }
可以查找jdk的安装目录下的demo\jfc\FileChooserDemo\src\ExampleFileFilter.java
/**
*Class <code> ExampleFileFilter </code> supply specify file type to be
*selected.
*/
public class ExampleFileFilter extends javax.swing.filechooser.FileFilter
{
/**
*The type of file can be accepted.
*/
public boolean accept(java.io.File f)
{
boolean accept = f.isDirectory();
if (!accept)
{
String suffix = getSuffix(f);
if (suffix != null)
accept = suffix.equals("desc");
}
return accept;
}
/**
*Get the extends name of select file.
*/
public String getDescription()
{
return "Description Files (*.desc)";
}
/**
*Get suffix of select file.
*/
private String getSuffix(java.io.File f)
{
String s = f.getPath();
String suffix = null;
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1)
suffix = s.substring(i + 1).toLowerCase();
return suffix;
}
}