Chapter 12. Chooser Dialogs
Just about any application you
write these days needs to have a mechanism for opening and saving
files. In the AWT, you can use the FileDialog
class, but that is a heavyweight dialog that lacks the flexibility of
the Swing components we’ve seen so far. The
JFileChooser is Swing’s answer to the
FileDialog. The Swing package also contains a
helper dialog for choosing colors (a common task in the configuration
area of applications). We’ll look at both of these dialogs in
this chapter.
Note
Some of the classes discussed in this chapter were still being updated as of JDK 1.2 beta4. You should check the online documentation that ships with the JDK for any late-breaking updates or additions.
To get things started, Figure 12.1 shows the class hierarchy of the pieces we’ll be looking at in this chapter.

Figure 12-1. Class hierarchy for JFileChooser and JColorChooser
The JFileChooser Class
Since it plays such an integral role in
just about every commercial application, let’s look at the file
chooser first. The JFileChooser class bundles a
directory pane and typical selection buttons into a handy interface.
Figure 12.2 shows the dialog window you get when you
select the Save option of a simple application. As you might expect,
other look-and-feels can also be applied to this chooser. Figure 12.3 shows the Windows and Motif L&Fs.
Figure 12-2. The JFileChooser save ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access