Java Reference
In-Depth Information
20.
else if (actionCommand.equals("Save")){
21.
editor.saveFile();
22.
}
23.
else if (actionCommand.equals("Exit")){
24.
editor.exitEditor();
25.
}
26.
else if (actionCommand.equals("Search")){
27.
editor.search();
28.
}
29.
else {
30.
System.out.println("Error: Unexpected action command.");
31.
}
32. }
33. }
12.2
File selection dialogues
File selection dialogues allow the user to select a file from the computer's mass
storage (hard disk). We will make this dialogue appear when the menu item 'Open'
is clicked. The file selected by the user is returned to the class that started the
dialogue.
12.2.1
The class JFileChooser
In Swing, file selection dialogues are predefined in the class JFileChooser .We
present only a few of its methods; the class offers a lot more possibilities. The
navigation in JFileChooser is (mostly) done as in the file selection dialogues of
the operating system. One can change directories, to choose a file from a list, or
by typing its name into a text field. There are buttons labelled 'Open' and 'Cancel'.
We do not have to implement any listener for these buttons. All the functions one
expects from a file selection dialogue are supplied by JFileChooser .
JFileChooser(String startDirectory);
int showOpenDialog(Component parent);
int showSaveDialog(Component parent);
File getSelectedFile();
JFileChooser(String startDirectory) generates a file selection dialogue
which is not yet visible. When it becomes visible the files in startDirectory
are listed, provided that this string specifies an existing directory.
showOpenDialog(Component parent) displays the file selection dialogue for
opening files. Component parent is usually the component in which the
showOpenDialog was called. This is important because a file selection dialogue
is modal ; see explanations below. On closing, a file selection dialogue returns an
integer value which is one of the following integer constants: APPROVE_OPTION
Search WWH ::




Custom Search