Listing Directory and File Information
Just as the FileViewer
class of Example 3-3 displays the contents
of a file in a TextArea
component,
the FileLister
class, shown in
Example 3-4, displays the
contents of a directory in a java.awt.List
component. When you select a
file or directory name from the list, the program displays information
(size, modification date, etc.) about the file or directory in a
TextField
component. When you
double-click on a directory, the contents of that directory are
displayed. When you double-click on a file, it displays the contents
of the file in a FileViewer
object.
Figure 3-2 shows a FileLister
window. Again, if you are not
already familiar with GUI programming in Java, don’t expect to
understand all of the code until you’ve read Chapter 11; instead, just pay attention
to the various uses of the File
object that are demonstrated in this example.
Figure 3-2. A FileLister window
The GUI mechanics of making the FileLister
work form a large part of this
example. The listDirectory( )
method lists the contents of a directory, using an optionally
specified FilenameFilter
object
passed to the FileLister( )
constructor. This object defines an accept(
)
method that is called for every entry in a directory to
determine whether it should be listed.
The itemStateChanged(
)
method is invoked when an item in the list is selected. It obtains information about ...
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.