Java Reference
In-Depth Information
A file chooser can be set to select files, directories, or both. You can determine which of these is allowed
by calling the
setFileSelectionMode()
method. The argument must be one of the constants
FILES
_
ONLY
,
DIRECTORIES
_
ONLY
, and
FILES
_
AND
_
DIRECTORIES
as defined in the
JFileChooser
class,
FILES
_
ONLY
being the default. You also have the
getFileSelectionMode()
method to enable you to determine what selection mode is set. To allow
multiple selections to be made from the list in the file dialog, you call the
setMultiSelectionEnabled()
method for your
JFileChooser
object with the argument
true
.
If you want the dialog to have a particular file selected when it opens, you can pass a
File
object
specifying that file to the
setSelectedFile()
method for the
JFileChooser
object. This will pre-
select the file in the file list for the dialog if the file already exists, and insert the name in the file name
field if it doesn't. The file list is created when the
JFileChooser
object is created but naturally files
may be added or deleted over time and when this occurs you will need to reconstruct the file list.
Calling the
rescanCurrentDirectory()
method before you display the dialog will do this for you.
You can change the current directory at any time by passing a
File
object specifying the directory to
the
setCurrentDirectory()
method.
That's enough detail for now. Let's put our customizing code together.
Try It Out - Creating a Customized File Dialog
We will first add a method to the
SketchFrame
class to create our customized file dialog and return
the
File
object corresponding to the file selected, or
null
if a file was not selected:
// Display a custom file save dialog
private File showDialog(String dialogTitle,
String approveButtonText,
String approveButtonTooltip,
char approveButtonMnemonic,
File file) { // Current file - if any
files.setDialogTitle(dialogTitle);
files.setApproveButtonText(approveButtonText);
files.setApproveButtonToolTipText(approveButtonTooltip);
files.setApproveButtonMnemonic(approveButtonMnemonic);
files.setFileSelectionMode(files.FILES
_
ONLY);
files.rescanCurrentDirectory();
files.setSelectedFile(file);
int result = files.showDialog(SketchFrame.this, null); // Show the dialog
return (result == files.APPROVE
_
OPTION) ? files.getSelectedFile() : null;
}
This method accepts five arguments that are used to customize the dialog - the dialog title, the button
label, the button tooltip, the shortcut character for the button, and the
File
object representing the file
for the current sketch. Each of the options is set using one of the methods for the
JFileChooser
object that we discussed earlier. The last argument is used to select a file in the file list initially. If it is
null
, no file will be selected from the file list.
Note that we reconstruct the file list for the dialog by calling the
rescanCurrentDirectory()
method. This is to ensure that we always display an up-to-date list of files. If we didn't do this, the dialog
would always display the list of files that were there when we created the
JFileChooser
object. Any
changes to the contents of the directory since then would not be taken into account.