I am putting a JFileChooser in my program, but that only takes images. So I decided to add filters:
import javax.swing.*;
public c
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]