JFileChooser filters

后端 未结 6 1597
悲&欢浪女
悲&欢浪女 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:54

    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");
    
    0 讨论(0)
  • 2020-11-29 08:54

    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";
            }
    
        });
    
    0 讨论(0)
  • 2020-11-29 08:55

    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());
    
    0 讨论(0)
  • 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]
    
    0 讨论(0)
  • 2020-11-29 09:08

    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());
    
    0 讨论(0)
  • 2020-11-29 09:14

    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

    0 讨论(0)
提交回复
热议问题