Java Reference
In-Depth Information
Figure 12.1 A file selection dialogue
indicates that the 'Open' (or 'Save', in case of a save dialogue) button of the
dialogue has been clicked.
CANCEL_OPTION
indicates that the 'Cancel' button
of the dialogue has been clicked.
showSaveDialog(Component parent)
displays the file selection dialogue for
sav-
ing
files; the rest is as for
showOpenDialog
.
getSelectedFile()
returns the selected file in a variable of type
File
if
AP-
PROVE_OPTION
has been selected.
Methods that display a file selection dialogue receive a
Component parent
as an
argument. Class
Component
from the AWT library is the mother class of all graph-
ical components in Java. The reference to the parent is needed because a
JFile-
Chooser
is modal. The parent component is blocked as long as it is visible itself.
Only after the dialogue is closed can the parent component be used again. This is
to avoid unwanted interactions such as modifying a file in the parent frame while
it is being saved.
Figure 12.1 shows the result of creating a
JFileChooser
with the
TestData
directory of the ITS package, a start directory, and then making it visible by
showOpenDialog
.
12.2.2
Adding the file chooser to the editor
We declare a variable
chooser
of type
JFileChooser
and a variable
selectedFile
of type
File
in class
EditorFrame
by
private
JFileChooser chooser;
private
File selectedFile;
Search WWH ::
Custom Search