Saturday, August 25, 2012

Swing:FileFilter


  • First of all create a subclass of FileFilter as follows:
  • Following subclass of FileFilter will allow the JFileChooser to show only gif files or folders.


public class GifFilter extends FileFilter{
   public boolean accept(File f){
      return f.getName().toLowerCase().endsWith(".gif") || f.isDirectory();
   }
   public String getDescription(){ return "GIF Image";}
}

Once we have a file filter object, we can use the setFileFilter() method of the JFileChooser class to install it into the JFileChooser object.
fileChooser.setFileFilter(new GifFilter());