Java Reference
In-Depth Information
Figur e 3−2. A FileLister window
Like the FileViewer class, the FileLister can be used by other classes, or it can
be invoked as a standalone program. If you invoke it standalone, it lists the con-
tents of the current directory. You can also invoke it with an optional directory
name to see the contents of that directory. Using the optional -e flag followed by
a file extension causes the program to filter the list of files and displays only the
ones that have the specified extension. Note how the main() method parses the
command-line arguments and uses an anonymous class to implement the File-
nameFilter interface.
Example 3−4: FileLister.java
package com.davidflanagan.examples.io;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.DateFormat;
import java.util.Date;
/**
* This class creates and displays a window containing a list of
* files and sub-directories in a specified directory. Clicking on an
* entry in the list displays more information about it. Double-clicking
* on an entry displays it, if a file, or lists it if a directory.
* An optionally-specified FilenameFilter filters the displayed list.
**/
public class FileLister extends Frame implements ActionListener, ItemListener {
private List list;
// To display the directory contents in
private TextField details;
// To display detail info in.
private Panel buttons;
// Holds the buttons
private Button up, close;
// The Up and Close buttons
private File currentDir;
// The directory currently listed
Search WWH ::




Custom Search