File Dialogs  «Prev  Next»


Lesson 2 Choosing files with file dialog boxes
Objective Investigate the steps necessary to use the FileDialog class.

Choosing Files using Dialog Boxes

The java.awt.FileDialog class is a subclass of java.awt.Dialog used for choosing a file to open or save. This class uses the host platform's standard Open and Save file dialog boxes.
java.awt.FileDialog extends java.awt.Dialog
java.awt.FileDialog extends java.awt.Dialog

The following page discusses choosing Files using file Dialog Boxes
You will not add components to a file dialog box or worry about how to handle user interaction. You will just retrieve the result, which will be the name and directory of a file. Since an applet cannot rely on having access to the file system, file dialog boxes are primarily useful in applications.
There are four steps to using a FileDialog object:

File related Class from AWT

I am going to jump out of the java.io package for a minute to pick up one file-related class from the AWT, java.awt.FileDialog. File dialogs are the standard open and save dialogs provided by the host GUI. Users use them to pick a directory and a name under which to save a file or to choose a file to open. The appearance varies from platform to platform, but the intent is the same.

FileDialog is a subclass of java.awt.Dialog that represents the native save and open dialog boxes:
public class FileDialog extends Dialog

A file dialog is almost completely implemented by a native peer. Your program doesn't add components to a file dialog or handle user interaction with event listeners. It just displays the dialog and retrieves the name and directory of the file the user chose after the dialog is dismissed.

Sun's HotJava Browser - Legacy

Since applets normally cannot read or write files, file dialogs are primarily useful only in applications. Nonetheless, there is no specific security manager check to see whether file dialogs are allowed.
During the dotcom era, Sun's applet viewer, HotJava (which has since been retired) , and other legacy browsers such as Netscape Navigator allowed untrusted applets to display file dialogs, retrieve the name and path of the file selected, and send that information back to the originating host over the network. Although this is a very minor security hole, since it only exposes the name and path of a single file selected by the user, it is still poses a security risk. Internet Explorer 4.0 and Navigator 4.0.3 and earlier did not allow applets to display file dialogs. Certainly, you cannot count on being allowed to use a file dialog in an applet, nor can you be guaranteed that it is not allowed either.

  1. Create the FileDialog object.
  2. Point the FileDialog at a default directory or file (optional).
  3. Make the FileDialog visible.
  4. Get the directory name and filename of the chosen file.
we will explore each of these steps in the next four lessons.

The package name for Swing has changed several times since its first early access release. javax.swing is its final home where this package name is used in Swing 1.1 (for use with JDK 1.1) and Java 2 (which includes Swing and the rest of JFC).