Java Reference
In-Depth Information
Choosing a File with JFileChooser
Problem
You want to allow the user to select a file by name using a traditional windowed file dialog.
Solution
Use a
JFileChooser
.
Discussion
The
JFileChooser
dialog provides a fairly standard file chooser. It has elements of both a
Windows chooser and a Mac chooser, with more resemblance to the former than the latter. If
you want to have control over which files appear, you need to provide one or more
FileFil-
ter
subclasses. Each
FileFilter
subclass instance passed into the
JFileChooser
's
ad-
dChoosableFileFilter()
method becomes a selection in the chooser's Files of Type:
choice. The default is All Files (
.
).
Figure 14-10
shows my demo program in action.