O'Reilly logo

Java Swing, 2nd Edition by Brian Cole, James Elliott, Marc Loy, Robert Eckstein, Dave Wood

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The File Chooser Package

Under javax.swing , you’ll find a package of helper classes for the JFileChooser. The javax.swing.filechooser package contains several classes for displaying and filtering files.

The FileFilter Class

The FileFilter class can be used to create filters for JFileChooser dialogs. The class contains only two abstract methods. It’s important to note that extensions are not the only way to judge a file’s fitness for a particular filter. The Mac filesystem, for example, can understand the creator of a file regardless of the file’s name. On Unix systems, you might write a filter to display only files that are readable by the current user.

Constructor

public FileFilter( )

The FileFilter class receives this default constructor at compile time; it is not defined in the class itself.

Filter methods

public abstract boolean accept(File f)

Return true if file f matches this filter. Note that you must explicitly accept directories (f.isDirectory( ) == true) if you want to allow the user to navigate into any subfolders.

public abstract String getDescription( )

Return a short description to appear in the filters pulldown on the chooser. An example would be “Java Source Code” for any .java files.

Figure 12-5 shows a file chooser with custom filters for multimedia file types.

A custom set of filters for use with JFileChooser

Figure 12-5. A custom set of filters for use with JFileChooser

Here’s the code for the application. Before we make ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required