Java Reference
In-Depth Information
Figure 9.2
An instance of
FileChooser
in action.
into stream sources and stream destinations and how to filter data going to a
histogram.
9.12 The
JFileChooser
dialog
To find files and directories on local media for I/O with a graphical user interface,
Swing provides the
JFileChooser
class. This file browser component (see
Figure 9.2) allows the user to search through directories and select one or more
files and directories, which are then available via instances of
File
.
The code snippet below illustrates how to use a
JFileChooser
to select files
for reading. The title, selection mode, and current directory are set with methods
from the class. The chooser allows for a filter that selects particular file types
for the list of files. This requires a subclass of the
FileFilter
class from the
javax.swing.filechooser
package. As shown with the class
JavaFil-
ter
,byoverriding the
accept()
method you can examine the current file or
directory and return
true
or
false
to accept or reject the current selection. You
can override
getDescription()
to return a string that describes the particular
file types that the filter selects.
...
This method uses a file chooser to locate a file to
read...
boolean openFile () {
Search WWH ::
Custom Search