Display a file from a FileDialog


In this Magercise, you will create an applet that creates and shows a FileDialog. It should use the file property of the FileDialog bean to obtain the selected file name and display it in a textfield on the applet.

An OS/2 C version of this Magercise exists in this directory. Note that the behavior of the C program may not be the same as the Magercise -- it is presented here for comparision against the Java application you will write in this Magercise. One of the major differences is that the C version does not support layout managers, so resizing a window will not resize the contents of that window.

Work Location

Perform all work for this magercise in VisualAge project MageLang Magercises, package magercises.displayfiledialog.

If this project does not appear in your Workspace add it from the repository (if it exists there) or create a new project using this name.

Tasks

Perform the following tasks:

  1. Create a new applet called DisplayFileDialog which contains a Button, TextLabel, TextField and a FileDialog bean.

  2. Create the connection to show the file dialog when the user presses the button.

  3. Create the connection to dispose of the file dialog.

    You may wonder why the Button both shows and disposes of the file dialog. Here's a brief synopsis of what happens. First, notice that there are two connections that both have actionPerformed(java.awt.event.ActionEvent) as the source event. One connection has the show() method as its target, and the other has the dispose() method as its target. Once the show() method is called in the file dialog, the file dialog has control until the user selects the Open button or the Cancel button. After the user has selected one of these buttons, control returns to the DisplayFileDialog class, and the next action it takes is to hide the file dialog by calling its dispose() method.

  4. Create the connection to set the TextField to the file that was chosen.

The task numbers above are linked to the step-by-step help page. Also available is a complete solution to the problem, and expected behavior, to demonstrate it.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.