class EDBFileFilter
extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
if (file.isDirectory() || file.getPath().toLowerCase().endsWith(".xml"))
return true;
else
return false;
} public String getDescription() {
return "Eproms xml files(.xml)";
}
}
JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new EDBFileFilter());
extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
if (file.isDirectory() || file.getPath().toLowerCase().endsWith(".xml"))
return true;
else
return false;
} public String getDescription() {
return "Eproms xml files(.xml)";
}
}
JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new EDBFileFilter());
public ThisFileFilter(String ext) {
this.ext=ext;
}
/* (非 Javadoc)
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
*/
public boolean accept(File file) {
// TODO 自动生成方法存根
if (file.isDirectory()) {
return true;
}
String fileName = file.getName();
int index = fileName.lastIndexOf(".");
if (index > 0 && index < fileName.length() - 1) {
String extendsion = fileName.substring(index + 1).toLowerCase();
if (extendsion.equals(ext)) {
return true;
}
}
return false;
} /* (非 Javadoc)
* @see javax.swing.filechooser.FileFilter#getDescription()
*/
public String getDescription() {
// TODO 自动生成方法存根
if(ext.equals("txt"))
{
return "文本文件(.txt)";
}
if(ext.equals("xls"))
{
return "EXCEL文件(.xls)";
}
return "";
}}实现FileFilter接口,这两个方法是必须覆写的 在调用的时候
jfc.addChoosableFileFilter(new ThisFileFilter("txt"));
jfc.addChoosableFileFilter(new ThisFileFilter("xls"));
* Customize the FileView which can link specifid files to icons accordingly
*
*/
class ShareFileView extends FileView {
private HashMap hash = new HashMap();
public ShareFileView() {
hash.put( "html",new AnOvalIcon( Color.red ) );
hash.put( "htm",new AnOvalIcon( Color.red ) );
hash.put( "xhtml",new AnOvalIcon( Color.red ) );
}
public String getName( File f ) {
String s = f.getName();
if( s.length() == 0 ) {
s = f.getAbsolutePath();
}
return s;
}
public String getDescription( File f ) {
return f.getName();
}
public String getTypeDescription( File f ) {
return f.getAbsolutePath();
}
public Icon getIcon( File f ) {
String path = f.getAbsolutePath();
int pos = path.lastIndexOf('.');
if( ( pos >= 0 ) && ( pos < ( path.length()-1 ) ) ) {
String ext = path.substring( pos+1 ).toLowerCase();
return (Icon)hash.get( ext );
}
return null;
}
public Boolean isTraversable( File file ) {
return ( new Boolean( file.isDirectory() ) );
}
} class ShareFileFilter extends FileFilter {
private String extensions[];
private String description; public ShareFileFilter( String description,String extension ) {
this( description, new String[] { extension } );
} public ShareFileFilter( String description,String extensions[] ) {
this.description = description;
this.extensions = (String[])extensions.clone();
} public boolean accept( File file ) {
if( file.isDirectory() ){
return true;
}
int count = extensions.length;
String path = file.getAbsolutePath();
for( int i = 0; i < count ; i++ ) {
String ext = extensions[i];
if( path.endsWith( ext ) &&
( path.charAt( path.length()-ext.length() ) == '.' ) ) {
return true;
}
}
return false;
} public String getDescription() {
return ( description == null? extensions[0]:description );
}
}
然后:
chooser = new JFileChooser( "." );
FileFilter type = new ShareFileFilter( "html files",
new String[]{ ".htm",".html","xhtml" } );
chooser.addChoosableFileFilter( type );
chooser.setFileFilter( type );
FileView view = new ShareFileView();
chooser.setFileView( view );