Java Reference
In-Depth Information
LISTING 10.13
continued
DISPLAY
The file chooser dialog box is displayed when the
showOpenDialog
method
is invoked. It automatically presents the list of files contained in a particular
directory. The user can use the controls on the dialog box to navigate to other
directories, change the way the files are viewed, and specify which types of files
are displayed.
The
showOpenDialog
method returns an integer representing the status of the
operation, which can be checked against constants defined in the
JFileChooser
class. In this program, if a file was not selected (perhaps by pressing the Cancel
button), a default message is displayed in the text area. If the user chose a file, it is
opened and its contents are read using the
Scanner
class. Note that this program
assumes the selected file contains text. It does not catch any exceptions, so if the
user selects an inappropriate file, the program will terminate when the exception
is thrown.
A text area component is defined by the
JTextArea
class. In this program, we
pass two parameters to its constructor, specifying the size of the text area in terms
of the number of characters (rows and columns) it should display. The text to
display is set using the
setText
method.
Search WWH ::
Custom Search