[null, {"document_url": "https://flylib.com/books/en/1.134.1/file_dialogs.html", "unformatted_src": "/books/1/134/1/html/2/images/javaio2_1801.jpg", "src": "https://flylib.com/books/1/134/1/html/2/images/javaio2_1801.jpg", "formatted_filename": "javaio"}, null, {"document_url": "https://flylib.com/books/en/1.134.1/file_dialogs.html", "unformatted_src": "/books/1/134/1/html/2/images/javaio2_1802.jpg", "src": "https://flylib.com/books/1/134/1/html/2/images/javaio2_1802.jpg", "formatted_filename": "javaio"}, null, null, null, {"document_url": "https://flylib.com/books/en/1.134.1/file_dialogs.html", "unformatted_src": "/aimg/5422-1.jpg", "src": "https://flylib.com/aimg/5422-1.jpg", "alt_text": "Java Network Programming, Third Edition"}, null, {"document_url": "https://flylib.com/books/en/1.134.1/file_dialogs.html", "unformatted_src": "/aimg/5422-2.jpg", "src": "https://flylib.com/aimg/5422-2.jpg", "alt_text": "Java Threads"}, null, {"document_url": "https://flylib.com/books/en/1.134.1/file_dialogs.html", "unformatted_src": "/aimg/5422-3.jpg", "src": "https://flylib.com/aimg/5422-3.jpg", "alt_text": "Java Nio"}, null, {"document_url": "https://flylib.com/books/en/1.134.1/file_dialogs.html", "unformatted_src": "/aimg/5422-4.jpg", "src": "https://flylib.com/aimg/5422-4.jpg", "alt_text": "Java Concurrency in Practice"}, null, null, {"document_url": "https://flylib.com/books/en/1.134.1/file_dialogs.html", "unformatted_src": "/media/images/top.png", "src": "https://flylib.com/media/images/top.png", "formatted_filename": "top"}]
File Dialogs


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. Figure 18-1 shows a standard Save dialog on the Mac; Figure 18-2 shows a standard open dialog on Linux.

Figure 18-1. The Mac's standard Save dialog Figure 18-2. Gnome Open dialog FileDialog is a subclass of java.awt.Dialog that represents the native save and open dialog boxes:

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.

To ask the user to select a file from a file dialog, perform these four steps:

File dialogs are modal. While the file dialog is shown, input to the parent frame is blocked, as with the parent frame of any modal dialog. The parent frame is normally the window from whose menu bar File/Open was selected. The parent frame is always set in the constructor:

Starting in Java 5, you can use another Dialog as the parent if you prefer:

Each FileDialog usually has a title. This is the prompt string for the file dialog, such as "Open File" or "Save Message As". This is also set in the constructor:

Finally, each FileDialog is either in open mode or save mode. The default for the previous constructors is open mode. However, this can be specified with one of these two constructors:

The mode argument is one of the two mnemonic constants FileDialog.LOAD or FileDialog.SAVE:

A typical invocation of this constructor might look like this:

In load mode, the user chooses an existing file. In save mode the user can either choose an existing file or create a new one.

Getter and setter methods allow the mode to be inspected and changed after the dialog is constructed:

To specify that the file dialog should appear with a particular directory opened or a particular file in that directory selected, you can invoke the setDirectory( ) and setFile( ) methods:


You make the file dialog visible by invoking the file dialog's setVisible(true) method, just like any other window:

As soon as the file dialog becomes visible, the calling thread stops and waits for the user to choose a file. The operating system takes over and handles user interaction until the user chooses a file or presses the Cancel button. At this point, the file dialog disappears from the screen, and normal program execution resumes.

Once the dialog has been dismissed, you can find out which file the user chose by using the file dialog's getdirectory( ) and getFile( ) methods:


If the user cancels the file dialog without selecting a file, getFile( ) and geTDirectory( ) return null. You should be ready to handle this, or you'll bump into a NullPointerException in short order.

Example 18-1 is a program that presents an open file dialog to the user and writes the contents of the file she selected on System.out.

Example 18-1. The FileTyper program

File dialogs only allow the user to select ordinary files, never directories. To ask users to pick a directory, you have to ask them to choose a file in that directory and then call getdirectory( ). Better yet, you can use a JFileChooser (discussed in the next section) that does allow the user to choose a directory.

A filename filter can be attached to a file dialog via the dialog's setFilenameFilter( ) method:

Once a file dialog's filename filter is set, it should only display files that pass through the filter. However, filename filters in file dialogs are only reliable on Unix (including Linux and Mac OS X). Windows is almost congenitally unable to support it because Windows' native file chooser dialog can only filter by file extension.

Example 18-2 demonstrates a simple filename filter that accepts files ending in .text, .txt, .java, .jav, .html, and .htm; all others are rejected.

Example 18-2. TextFilter

This program demonstrates one problem of relying on file extensions to determine file type. Many other file extensions indicate text files, for example, .c, .cc, .pl, .f, and many more. Furthermore, many text files, especially those on Macintoshes, have no extension at all. This program completely ignores all those files.

You do not necessarily have to write a new subclass for each different file filter. Example 18-3 demonstrates a class that can be configured with different lists of filename extensions. Every file with an extension in the list passes the filter. Others don't.

Example 18-3. ExtensionFilenameFilter

This class is designed to filter files by extension. You configure which extensions pass the filter when you create the object or by calling addExtension( ). This avoids excessive proliferation of classes.

Basic I/O

Introducing I/O

Output Streams

Input Streams


File Streams

Network Streams

Filter Streams


Print Streams


Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O



Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers


Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text


The Java Communications API


The J2ME Generic Connection Framework


Character Sets

show all menu
 Beginners Guide to DarkBASIC Game Programming (Premier Press Game Development) Java Network Programming, Third Edition Java Threads Java Nio Java Concurrency in Practice Crystal Reports 9 on Oracle (Database Professionals)
A+ Fast Pass
Oracle Developer Forms Techniques
Adobe After Effects 7.0 Studio Techniques
Making Sense of Change Management: A Complete Guide to the Models, Tools and Techniques of Organizational Change
DNS & BIND Cookbook
Flylib.com © 2008-2017.
If you may any questions please contact us: email@example.com