How to select default FileFilter when creating a JFileChooser dialog?
Asked Answered
T

2

10

Good afternoon all,

I have an instance of a javax.swing.JFileChooser and I added a few choosable javax.swing.FileFilters using addChoosableFileFilter.

However when I show the dialog (through showSaveDialog), my custom choosable FileFilter is not selected by default.

I wanted to select the filter "Image Files" by default..

How do we select a default filter for the javax.swing.JFileChooser dialog?

SSCCEd:

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new java.lang.Runnable() {
        @Override
        public void run() {
            new javax.swing.JFrame() {
                {
                    final javax.swing.JButton button = new javax.swing.JButton("Save");
                    button.addMouseListener(new java.awt.event.MouseAdapter() {
                        @Override
                        public void mousePressed(java.awt.event.MouseEvent e) {
                            new javax.swing.JFileChooser() {
                                {
                                    addChoosableFileFilter(NewFileFilter("Text Files", new String[] { "txt" }));
                                    addChoosableFileFilter(NewFileFilter("Image Files", new String[] { "gif", "png", "jpg" }));
                                    addChoosableFileFilter(NewFileFilter("Audio Files", new String[] { "mp3" }));
                                    addChoosableFileFilter(NewFileFilter("Video Files", new String[] { "mp4", "avi" }));
                                    showSaveDialog(button);
                                }

                                private javax.swing.filechooser.FileFilter NewFileFilter(final String desc, final String[] allowed_extensions) {
                                    return new javax.swing.filechooser.FileFilter() {
                                        @Override
                                        public boolean accept(java.io.File f) {
                                            if (f.isDirectory()) {
                                                return true;
                                            }
                                            int pos = f.getName().lastIndexOf('.');
                                            if (pos == -1) {
                                                return false;
                                            } else {
                                                String extension = f.getName().substring(pos + 1);
                                                for (String allowed_extension : allowed_extensions) {
                                                    if (extension.equalsIgnoreCase(allowed_extension)) {
                                                        return true;
                                                    }
                                                }
                                                return false;
                                            }
                                        }

                                        @Override
                                        public String getDescription() {
                                            return desc;
                                        }
                                    };
                                }
                            };
                        }
                    });
                    add(button);
                    setSize(100, 100);
                    setVisible(true);
                }
            };
        }
    });
}
Tenancy answered 20/2, 2012 at 10:19 Comment(0)
L
18

Using setFileFilter():

FileFilter ff = NewFileFilter("Image Files", new String[] { "gif", "png", "jpg" });
addChoosableFileFilter(ff);
setFileFilter(ff);

Why are you subclassing the file chooser, rather than just using it? And why aren't you respecting Java's naming conventions (methods start with a lower-case letter)? And why don't you respect Swing's threading policy, which say that Swing components must be used in the EDT only.

Liking answered 20/2, 2012 at 10:27 Comment(3)
Added javax.swing.SwingUtilities.invokeLater in the example. Why should methods be camelCased and not PascalCased?Tenancy
Code Conventions for the JavaKorella
Because that's the standard convention in Java.Liking
F
0
FileFilter filter = new FileNameExtensionFilter("Data Files", "*.dat"); 
JFileChooser fc   = new JFileChooser(); 
fc.addChoosableFileFilter(filter); 
fc.setMultiSelectionEnabled(false);
Foret answered 4/8, 2017 at 12:8 Comment(1)
Please format your code properly, an add some details to your answer.Commissioner

© 2022 - 2024 — McMap. All rights reserved.