JFileChooser filters

You are using wrong ImageFiler class :-)

The ImageFilter from tutorial is not from java.awt package you are importing. This ImageFilter must implement javax.swing.filechooser.FileFilter.

Please check if there is other ImageFilter class defined in tutorial and use it.

Example of proper filefilter:

new JFileChooser().addChoosableFileFilter(new FileFilter() {

        @Override
        public boolean accept(File f) {
            // TODO Auto-generated method stub
            return f.getName().endsWith(".jpg");
        }

        @Override
        public String getDescription() {
            return "JPEG files";
        }

    });

I am putting a JFileChooser in my program, but that only takes images.

For a list of types supported by that JRE on that OS, use ImageIO.

FileFilter imageFilter = new FileNameExtensionFilter(
    "Image files", ImageIO.getReaderFileSuffixes());

Types seen - Java 1.6/Windows 7

bmp
jpg
jpeg
wbmp
png
gif

Note: don't hard-code that list! It might change from version to version, and OS to OS. E.G.

  1. I am not surprised that Windows has support to load BMP, but does that come up in a Mac?
  2. Seeing WBMP alerted me to the existence of such a format!

That list would have many more formats if jai was installed.

Filter as it appears in a chooser

Image Chooser


the argument of fc.addChoosableFileFilter() should be a subclass of javax.swing.filechooser.FileFilter. For example, you can change your code as

fc.addChoosableFileFilter(new FileNameExtensionFilter("Image Files", "jpg", "png", "tif");

i am using setFileFilter().

My Code is Below (JAVA-JSE 1.6)

JFileChooser c = new JFileChooser();
//Setting Up The Filter
FileFilter imageFilter = new FileNameExtensionFilter(
    "Image files", ImageIO.getReaderFileSuffixes());

//Attaching Filter to JFileChooser object
c.setFileFilter(imageFilter);

//Displaying Filechooser
int rVal = c.showOpenDialog(new JPanel());