JFileChooser filters

后端 未结 6 1619
悲&欢浪女
悲&欢浪女 2020-11-29 08:14

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

Code

import javax.swing.*;

public c         


        
6条回答
  •  时光说笑
    2020-11-29 08:58

    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]
    

提交回复
热议问题