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.


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

Get Java Swing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.