Using a custom FileFilter with a JFileChooser

Chris V. picture Chris V. · Jan 28, 2011 · Viewed 11.3k times · Source

I need to filter files in a filechooser that only allows image files to be chosen. I cant seem to figure out whats wrong with my code here:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;


public class Viewer extends JFrame implements ActionListener{
 /**
  * 
  */
 private static final long serialVersionUID = 1L;

 private JMenuItem open;
 private JMenuItem exit;
 private JFileChooser fileChooser;
 private JLabel image;

 public Viewer(){
  super("Picture Viewer");
  this.setLayout(new BorderLayout());
  //this.setSize(400, 400);

  JPanel canvas = new JPanel();
  this.add(canvas, BorderLayout.CENTER);
  image = new JLabel();
  canvas.add(image, BorderLayout.CENTER);

  JMenuBar menubar = new JMenuBar();
  this.add(menubar, BorderLayout.NORTH);
  JMenu menu = new JMenu("File");
  menubar.add(menu);
  open = new JMenuItem("Open...");
  open.addActionListener(this);
  menu.add(open);
  exit = new JMenuItem("Exit");
  exit.addActionListener(this);
  menu.add(exit);

  this.setVisible(true);
  this.pack();
 }

 public void actionPerformed(ActionEvent e){
  if(e.getSource() == open){
   fileChooser = new JFileChooser();
   fileChooser.showOpenDialog(this);
   fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
   fileChooser.setFileFilter(new ImageFileFilter());

    int returnVal = fileChooser.showOpenDialog(null);
          if(returnVal == JFileChooser.APPROVE_OPTION) {
              File file = fileChooser.getSelectedFile();
              // bmp, gif, jpg, png files okay
              BufferedImage bi;
     try {
      bi = ImageIO.read(file);
               image.setIcon(new ImageIcon(bi));
     } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
     }
              // catch IOException
          }
      this.pack();
  }
  else if(e.getSource() == exit){
   System.exit(0);
  }
 }

 public static void main(String[] args){
  Viewer viewer = new Viewer();
 }
 public class ImageFileFilter implements FileFilter{
   private final String[] okFileExtensions = 
     new String[] {"jpg", "png", "gif", "bmp"};

   public boolean accept(File file)
   {
     for (String extension : okFileExtensions)
     {
       if (file.getName().toLowerCase().endsWith(extension))
       {
         return true;
       }
     }
     return false;
   }
 }
}

It's telling me that my custom filter class that implements FileFilter isnt of type FileFilter. :/

Answer

Pace picture Pace · Jan 28, 2011

The JFileChooser needs you to extend an instance of javax.swing.filechooser.FileFilter. Since you have implements your IDE is importing java.io.FileFilter instead.