Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

java - JFileChooser filters

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...