Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

Displaying Files

 

Training Index

by jGuru

Help  API Docs  About This Short Course  Short Course  Exercises

This exercise produces an application rather than an applet. This application should be run from the command line rather than from a page in a browser. (Applets cannot do file I/O for security reasons.)

Open a Frame with a list box and a text area. Using routines provided for you, display the list of files in the current directory in the list box. When the user double-clicks on an entry, display that file in the text area.

Skeleton Code

Tasks

1. Create class DirectoryList as subclass of Frame. In the constructor, set the size to 400 x 500, create the list box in the north position, add the elements passed in to the constructor, and create a text area in the center position.

2. Add the following code to DirectoryList so that you can shut down the application window:

import java.awt.event.*;
...
// handle the WINDOW events here
addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent event) {
    setVisible(false);
    dispose();
    System.exit(0);
  }
});

3. Add an actionPerformed() method to handle selection of the list elements. Upon selection (with a double-click operation), call displayFile() with the filename selected.

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

Demonstration

This program cannot be run as an applet as it uses file I/O. Please run the solution from the command line as an application, or from an IDE window.

Next Exercise

Exercises

Short Course

Copyright 1996-2000 jGuru.com. All Rights Reserved.