Using a custom FileFilter with a JFileChooser
Asked Answered
U

3

5

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

Uncertainty answered 28/1, 2011 at 19:28 Comment(0)
V
3

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.

Vaisya answered 28/1, 2011 at 19:48 Comment(4)
Thanks, I did that and now it works, but by default the directory is 'Documents' in my filechooser, so no images are there obviously. When I try to navigate to another directory, they wont show. How can I make the default directory when the filechooser opens set to my Picture folder?Uncertainty
Use the setCurrentDirectory method of JFileChooser. Are you sure your filter isn't just rejecting all possible files? I would put a breakpoint in the accept method to test.Vaisya
This is what I ferreted out too.Raeannraeburn
You should accept answers, if they solve your problem!Generatrix
O
3

Your file filter should accept directories as well.

if (file.isDirectory())
    return true;

even tho your file selection mode is files only (which is correct).

Ovary answered 29/1, 2011 at 4:45 Comment(0)
G
3

Since Java7 you can simply use FileNameExtensionFilter(String description, String... extensions) instead of subclassing FileFilter.

A simple JFileChooser analog to the example would be:

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("Image files", "jpg", "png", "gif", "bmp"));

I know the question was answered long ago, but this is actually the simplest solution.

Generatrix answered 31/7, 2014 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.