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
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
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
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 ...