JFileChooser filters
Asked Answered
S

6

22

I am putting a JFileChooser in my program, but that only takes images. So I decided to add filters:

Code

import javax.swing.*;

public class fileChooser {

 public static void main(String[] args) {
    JPanel panel = new JPanel();
    
    final JFileChooser fc = new JFileChooser();
    int file = fc.showOpenDialog(panel);
    fc.addChoosableFileFilter(new ImageFilter());
    fc.setAcceptAllFileFilterUsed(false);
 }
}

I got that straight from the Java tutorials. But Eclipse underlines the following as an error:

fc.addChoosableFileFilter(new ImageFilter());
fc.setAcceptAllFileFilterUsed(false);

Any suggestions?

Sialkot answered 22/11, 2012 at 17:43 Comment(3)
and the error is?? I usually use JFileChooser#setFileFilter(..)Emilie
ImageFilter is contained in java.awt.image.ImageFilter which you did not importOperate
I just put together some code above, but I did java that import in my real code. Sorry for the trouble!Sialkot
F
34

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 was installed.

Filter as it appears in a chooser

Image Chooser

Fioritura answered 23/11, 2012 at 0:5 Comment(0)
F
10

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");
Feltonfelts answered 22/11, 2012 at 17:54 Comment(0)
H
6

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());
Harmless answered 14/4, 2014 at 6:45 Comment(1)
i think i missed the import part... don't u think so?Rabblerousing
S
3

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";
        }

    });
Septenary answered 22/11, 2012 at 17:50 Comment(3)
Here is the class you are looking for: docs.oracle.com/javase/tutorial/uiswing/examples/components/…Gestapo
Note that this does not match .JPG extensions, in this example. Assuming you are on a case insensitive file system, you should do f.getName().toLowerCase().endsWith(".jpg") instead.Marbleize
This rules out directories on Windows, you should use FileNameExtensionFilterTaranto
S
1

You can use FileFilter class and then use setFileFilter()

class ImageFilter extends FileFilter {

@Override
public boolean accept(File pathname) {
  String filename = pathname.getName();
  if (pathname.isDirectory()) {
    return true;

  } else if (filename.endsWith("jpg'") || filename.endsWith("jpeg") || filename.endsWith("png") || filename.endsWith("gif")) {
    return true;
  } else {
    return false;
  }
}

Now in your main class:

fc.setFileFilter(new ImageFilter());
Saxton answered 24/7, 2017 at 4:9 Comment(0)
G
1

The accepted answer (using FileNameExtensionFilter with ImageIO.getReaderFileSuffixes()) has some problems.

On my system (jdk1.8.0_192 on Ubuntu) ImageIO.getReaderFileSuffixes() returns an array like this:

[, jpg, tiff, bmp, pcx, gif, png, ppm, tif, pgm, wbmp, jpeg, pbm]

Note the first empty String. This String is not valid in FileNameExtensionFilter:

IllegalArgumentException: Each extension must be non-null and not empty

A solution to this would be to remove the empty String - using Apache commons-lang:

String[] extensions = ArrayUtils.removeAllOccurences(ImageIO.getReaderFileSuffixes(), "");
FileFilter filter = new FileNameExtensionFilter("Images", extensions);

On a side note - on the same system with openjdk version "11.0.2" 2019-01-15 I get these extensions:

[jpg, tif, tiff, bmp, gif, png, wbmp, jpeg]
Greyhen answered 10/5, 2019 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.