Java Reference
In-Depth Information
■
Tip
Setting the
FileFilter
with
setFileFilter()
before adding filters with
add
ChoosableFileFilter()
causes the accept-all-file filter to be unavailable. To put it back, call
setAcceptAllFileFilterUsed(true)
. In addition, you can reset the filter list with a call to
resetChoosableFileFilters()
.
One built-in filter isn't a
FileFilter
. It concerns hidden files, such as those that begin with
a period (
.
) on UNIX file systems. By default, hidden files aren't shown within the
JFileChooser
.
To enable the display of hidden files, you must set the
fileHidingEnabled
property to
false
:
aFileChooser.setFileHidingEnabled(false);
■
Tip
When creating
javax.swing.filechooser.FileFilter
subclasses, you may want to have the
new class also implement the
java.io.FileFilter
interface. To do this, simply add
implements
java.io.FileFilter
to the class definition. This works because the method signature for the
accept()
method in the
javax.swing.filechooser
class matches the interface definition:
public boolean
accept(File file)
.
Choosing Directories Instead of Files
The
JFileChooser
supports three selection modes: files only, directories only, and files and
directories. The
fileSelectionMode
property setting determines the mode of the chooser. The
available settings are specified by the three
JFileChooser
constants:
FILES_ONLY
,
DIRECTORIES_ONLY
,
and
FILES_AND_DIRECTORIES
. Initially, the file chooser is in
JFileChooser.FILES_ONLY
mode.
To change the mode, just call
public void setFileSelectionMode(int newMode)
.
In addition to the
fileSelectionMode
property, you can use the read-only
fileSelectionEnabled
and
directorySelectionEnabled
properties to determine the type
of input currently supported by the file chooser.
Adding Accessory Panels
The
JFileChooser
supports the addition of an accessory component. This component can
enhance the functionality of the chooser, including previewing an image or document, or
playing an audio file. To respond to file selection changes, the accessory component should
attach itself as a
PropertyChangeListener
to the
JFileChooser
. When the
JFileChooser.
➥
SELECTED_FILE_CHANGED_PROPERTY
property changes, the accessory then changes to reflect the
file selection. Figure 9-28 shows how an image previewer accessory component might appear.
Configuring the accessory for a chooser is just like setting any other property.
fileChooser.setAccessory(new LabelAccessory(fileChooser));