JFileChooser filters

烈酒焚心 提交于 2019-11-27 01:51:28

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

mazaneicha

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());

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

    });

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());

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]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!