Java Reference
In-Depth Information
This automatically creates a file save dialog with the
SketcherFrame
object as parent, and with Save and
Cancel buttons. The
SketcherFrame.this
notation is used to refer to the
this
member for the
Sketcher-
Frame
object from within a method of an inner class object of type
FileAction
. Just to remind you, you
reference the
this
variable for an outer class object from a non-static inner class object by qualifying
this
with the outer class name. The file chooser dialog is displayed centered in the parent component, which is
the
SketcherFrame
object here. If you specify the parent component as
null
, the dialog is centered on the
screen. This also applies to all the other methods I discuss that display file chooser dialogs.
Displaying a File Open Dialog
When you need a file open dialog, you can call the
showOpenDialog()
member of a
JFileChooser
object.
Don't be fooled here, though. A save dialog and an open dialog are essentially the same. They differ only
in minor details — the title bar and one of the button labels. The sole purpose of both dialogs is simply to
enable the user to select a file — for whatever purpose. If you want to be perverse, you could pop up a save
dialog to open a file and vice versa!
You can also display a customized dialog from a
JFileChooser
object. Although it's not strictly neces-
sary for the Sketcher application — the standard file dialogs are quite adequate — you adopt a custom ap-
proach so you get some experience of using a few more
JFileChooser
methods.
You display a custom dialog by calling the
showDialog()
method for the
JFileChooser
object supply-
ing two arguments. The first argument is the parent component for the dialog window, and the second is the
approve button text — the approve button being the button that you click to expedite the operation rather
than cancel it. You could display a dialog with an Open button with the following statement:
int result = fileChooser.showDialog(SketcherFrame.this, "Open");
If you pass
null
as the second argument here, the button text is whatever was set previously —
possibly the default. The value that the method returns can be one of three constants that are defined in the
JFileChooser
class:
•
APPROVE_OPTION
if the approve button was clicked
•
CANCEL_OPTION
if the cancel button was clicked
•
ERROR_OPTION
if an error occurred or if the dialog window was closed
You can compare the value that
showDialog()
returns with these constants to determine how the dialog
was closed.
Customizing a File Chooser Dialog
Before you display a custom dialog, you would normally do a bit more customizing of what is to be dis-
played by the dialog. You use the following
JFileChooser
methods to customize the dialogs for Sketcher:
•
setDialogTitle(String text)
: Sets the
String
object that you pass as the argument as the
dialog's title bar text.
•
setApproveButtonText(String text)
: Sets the
String
object that you pass as the argument as
the approve button label.
•
setApproveButtonToolTipText(String text)
: Sets the
String
object that you pass as the ar-
gument as the approve button tooltip.