home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book Home Java Enterprise in a Nutshell Search this book

3.16. JFileChooser

javax.swing.JFileChooser is a specialized component that allows the user to browse the filesystem and select a file. The easiest way to use it is with the showOpenDialog() and showSaveDialog() methods. These methods differ only in the text that appears in the "Okay" button. You can also call the showDialog() method and specify your own text for that button. Each of these methods returns an integer status code that specifies how the user dismissed the dialog. If the return value is APPROVE_OPTION, the user actually selected a file, which you can obtain with the getSelectedFile() method. For example:

public void saveAs() {
  JFileChooser chooser = new JFileChooser();
  int result = chooser.showSaveDialog(mainpane);
  if (result == JFileChooser.APPROVE_OPTION)


Note that showSaveDialog() and showOpenDialog() are instance methods, not static methods like those used with JOptionPane. This means that you can customize the dialog by setting properties on your JFileChooser object. You may be interested in setting the currentDirectory and fileSelectionMode properties before you display a JFileChooser. fileSelectionMode can be set to FILES_ONLY, DIRECTORIES_ONLY, or FILES_AND_DIRECTORIES. Once you create a JFileChooser for an application, you may want to reuse it, rather than creating a new one each time you need one. If you do so, the JFileChooser automatically remembers the currentDirectory most recently selected by the user.

3.16.1. Using File Filters

The javax.swing.filechooser package defines auxiliary classes that are used by JFileChooser. One of the most important of these is FileFilter. The abstract javax.swing.filechooser.FileFilter class is much like the java.io.FileFilter interface. Each defines an accept() method that is passed File objects and returns true for each file that should be displayed. The FileFilter class used by JFileChooser has an additional getDescription() method that returns a string that names the types of files accepted by the filter. For example, you might define a FileFilter subclass that accepts files with names ending in .htm or .html and returns a description of "HTML Files."

When you create a JFileChooser, you can specify the FileFilter it is to use with setFileFilter(). Alternately, you can specify an array of FileFilter objects with setChoosableFileFilters(). In this case, JFileChooser displays the descriptions of the filters and allows the user to choose one.

3.16.2. Customizing JFileChooser

The behavior of a JFileChooser can be customized by providing your own implementation of FileView and FileSystemView. Both of these abstract classes are defined in the javax.swing.filechooser class. FileView defines methods that affect the way individual files are displayed by the JFileChooser, while FileSystemView defines methods that enable the JFileChooser to handle operating-system dependencies in the filesystem. FileSystemView understands the notion of hidden files, and it can return a complete list of filesystem roots, a capability that was lacking from the basic java.io.File class prior to Java 1.2. The default FileView and FileSystemView classes provided by JFileChooser are perfectly adequate for most purposes, so you typically don't have to implement these classes yourself.

It is also possible to customize a JFileChooser by providing an accessory component. If you pass a JComponent to the setAccessory() method of JFileChooser, the Swing component you specify is displayed in the file chooser dialog box. A common use of a file chooser accessory is as a file preview component. In order to provide a preview of the currently selected file, the accessory must know what the currently selected file is. It can get this information by implementing the PropertyChangeListener interface and listening for changes to the selectedFile property. In order for this to work, you have to pass the accessory object to the addPropertyChangeListener() method of the JFileChooser, of course.

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.