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. (More classes are planned
for this package, but they are currently located in the file chooser
demo area in the examples/ directory of the
Swing download package.)
The FileFilter Class
The FileFilter class
can be used to create filters for JFileChooser
dialogs. The class contains only two abstract methods, but default
implementations based on filename extensions should become a standard
part of the Swing package with the next release. 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.
Constructors
- public FileFilter()
The
FileFilterclass receives this default constructor at compile time, it is not defined in the class itself.
Filter Methods
- public abstract boolean accept(File f)
Returns
trueif the filefmatches this filter.- public abstract String getDescription()
Returns a short description to appear in the filters pull-down on the chooser. An example would be “Java Source Code” for any
.javafiles.
Figure 12.5 shows a file chooser with custom filters for multimedia file types.
Figure 12-5. A custom set of filters for use ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access