Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter file type in FileDialog?

I am using FileDialog for saving and loading in a Java program.

How can I filter the dropdown list to specify the file type as "JPG" or "JPEG" etc. ?

I have tried the following code, but it seems to have no effect.

Are there any other ways of doing this ?

public void actionPerformed(ActionEvent e) {
            FileDialog saveFileDialog = new FileDialog(new Frame(), "Save", FileDialog.SAVE);

            saveFileDialog.setFilenameFilter(new FilenameFilter(){
                @Override
                public boolean accept(File dir, String name) {
                    return name.endsWith(".jpg") || name.endsWith(".jpeg");
                }
            });
            saveFileDialog.setFile("Untitled.jpg");
            saveFileDialog.setVisible(true);
        }
like image 990
PinkiePie-Z Avatar asked Sep 24 '12 03:09

PinkiePie-Z


1 Answers

The answer is simple. You can use

 saveFileDialog.setFile("*.jpg;*.jpeg");

No need to use setFilenameFilter method. You can add as many file type as you which.

Solving of this problem can be found in huxhorn' s comment in Bug ID: 4031440 FileDialog doesn't call FilenameFilter.accept().

public void actionPerformed(ActionEvent e) {
            FileDialog saveFileDialog = new FileDialog(new Frame(), "Save", FileDialog.SAVE);
            saveFileDialog.setFile("*.jpg;*.jpeg");
            saveFileDialog.setVisible(true);
        }
like image 118
swemon Avatar answered Oct 12 '22 05:10

swemon