Java Reference
In-Depth Information
USING A FILE CHOOSER
The
JFileChooser
class in the
javax.swing
package provides an easy-to-use mechanism for creating file
dialogs for opening and saving files. The
JFileChooser
class is slightly out of kilter with the new I/O cap-
ability in that it uses
java.io.File
objects to encapsulate file paths, rather than
java.nio.file.Path
ob-
jects that you use with the new I/O. A
File
object encapsulates an absolute or relative path string for a file
or directory, similar to a
Path
object. Producing
Path
objects from
File
objects and vice versa is very easy,
so having to work with both
File
and
Path
objects doesn't represent a problem.
You can use a single object of this class to create all the file dialogs you need, so you can add a member
to the
SketcherFrame
class now to store a reference to a
JFileChooser
object that you create in the con-
structor:
private JFileChooser fileChooser;
// File chooser dialog
There are several
JFileChooser
constructors, but I'm discussing only a couple of them here. The default
constructor creates an object with the current directory as the default directory, but that won't quite do for
our purposes. What you want in the first instance is for the default directory to be the one specified by the
DEFAULT_DIRECTORY
path that you defined in the
SketcherConstants
class. There is no
JFileChooser
constructor that accepts a
Path
object reference, so you use the constructor that accepts an argument of type
String
that specifies the directory that the dialog displays initially. You can call the
toString()
method
for
DEFAULT_DIRECTORY
to get the path as a
String
and pass that to the constructor that accepts a
String
reference to specify the directory. Add the following statement to the
SketcherFrame
constructor, following
the statements that you added earlier that verified
DEFAULT_DIRECTORY
actually existed on the hard drive:
fileChooser = new JFileChooser(DEFAULT_DIRECTORY.toString());
The dialog that the
fileChooser
object encapsulates displays the contents of the directory specified by
DEFAULT_DIRECTORY
. The dialog has two buttons, one to approve a selection from the directory contents and
the other to cancel the dialog. You can now use the
fileChooser
object to implement the event handling
for the
File
menu. During use of the dialog, the user may well change the current directory to something
else. As long as you are using the same
JFileChooser
object to display the dialogs for choosing a file, the
current directory is remembered between displaying one file chooser dialog and the next.
There are a considerable number of methods in the
JFileChooser
class, so rather than trying to discuss
them all, which would take many pages of text and be incredibly boring, I'm leaving you to explore these at
your leisure and will just introduce the ones that you can apply to Sketcher to support the
File
menu.
Displaying a File Save Dialog
In most cases you'll want to display a modal file save dialog when the Save menu item or toolbar button is
selected. As luck would have it, the
JFileChooser
class has a
showSaveDialog()
method that does pre-
cisely what you want. All you have to do is call the method with a reference to the
Component
object that
is the parent for the dialog to be displayed as the argument. The method returns a value indicating how the
dialog was closed. You could display a save dialog in a method for a
FileAction
object with the following
statement:
int result = fileChooser.showSaveDialog(SketcherFrame.this);