O'Reilly logo

Java Swing by Dave Wood, Marc Loy, Robert Eckstein

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

Class hierarchy for JFileChooser and JColorChooser

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

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