Java Reference
In-Depth Information
This will automatically create a File Save dialog with the SketchFrame object as parent, and Save and
Cancel buttons. The SketchFrame.this notation is used to refer to the this pointer for the
SketchFrame object from within a method of an inner class object of type FileAction . The file
chooser dialog will be displayed centered in the parent component - our SketchFrame object here. If
you specify the parent component as null , the dialog will be centered on the screen. This also applies
to all the other methods we will discuss that display file chooser dialogs.
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
only differ in minor details - the title bar and one of the button labels. The sole purpose of both dialogs
is simply to select a file - for whatever purpose. If you wanted to be perverse, you could pop up a save
dialog to open a file and vice versa!
You also have the ability to display a customized dialog from a JFileChooser object. Although it's
not strictly necessary for us here - we could make do with the standard file dialogs - we will adopt a
custom approach, as it will give us some experience of using a few more JFileChooser methods.
You can display a dialog by calling the showDialog() method for the JFileChooser object
supplying two arguments. The first 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 on to expedite the
operation rather than cancel it. You could display a dialog with a Save button with the statement:
int result = files.showDialog(SketchFrame.this, "Save");
If you pass null as the second argument here, the button text will be whatever was set previously -
possibly the default.
Before you display a custom dialog, though, you would normally do a bit more customizing of what is to
be displayed. We will be using the following JFileChooser methods to customize our dialogs:
Method
Description
setDialogTitle()
The String object passed as an argument is
set as the dialog title bar text.
setApproveButtonText()
The String object passed as an argument is
set as the approve button label.
setApproveButtonToolTipText()
The String object passed as an argument is
set as the approve button tooltip.
setApproveButtonMnemonic()
The character passed as an argument is set as
the approve button mnemonic defining a
shortcut. The shortcut will appear as part of
the tooltip.
Search WWH ::




Custom Search