O'Reilly logo

Java I/O by Elliotte Rusty Harold

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 13. File Dialogs and Choosers

Filenames are problematic, even if you don’t have to worry about cross-platform idiosyncrasies. Users forget filenames, mistype them, can’t remember the exact path to files they need, and more. The proper way to ask a user to select a file is to show them a list of the files in the current directory and get them to select from that list. You also need to allow them to navigate between directories, insert and remove floppy disks, mount network servers, and more.

Most graphical user interfaces (and not a few nongraphical ones) provide standard widgets for selecting a file. In Java the platform’s native file selector widget is exposed through the java.awt.FileDialog class. Like many native peer-based classes, however, FileDialog doesn’t behave exactly the same on all platforms. Therefore, Swing (part of the Java Foundation Classes) provides a pure Java implementation of a file dialog, the javax.swing.JFileChooser class.[25] JFileChooser (and Swing in general) has much more reliable cross-platform behavior.

File Dialogs

I’m 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. Figure 13.1 shows a standard Save dialog on the Mac; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required