O'Reilly logo

Java Examples in a Nutshell, 3rd Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

A FileLister window

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required