Tuesday, April 19, 2011

Adding a filter to file chooser dialog

Eg. This is filter for .java files.
JFileChooser fileChooser = new JFileChooser(new File(filename));
fileChooser.addChoosableFileFilter(new MyFilter());

// Open file dialog.
fileChooser.showOpenDialog(frame);
openFile(fileChooser.getSelectedFile());

class MyFilter extends javax.swing.filechooser.FileFilter {
    public boolean accept(File file) {
        String filename = file.getName();
        return filename.endsWith(".java");
    }
    public String getDescription() {
        return "*.java";
    }
}

No comments:

Post a Comment

Chitika