Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a custom FileFilter with a JFileChooser

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. :/

like image 474
Chris V. Avatar asked Jan 28 '11 19:01

Chris V.


People also ask

Which of the following is built in dialog that lets a user select a file JFileChooser?

The object of JFileChooser class represents a dialog window from which the user can select file.

What is JFileChooser in Java?

JFileChooser is a part of java Swing package. The java Swing package is part of JavaTM Foundation Classes(JFC) . JFC contains many features that help in building graphical user interface in java . Java Swing provides components such as buttons, panels, dialogs, etc .


1 Answers

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.

like image 125
Pace Avatar answered Oct 04 '22 04:10

Pace