Chapter 27. The javax.swing.filechooser PackageThe javax.swing.filechooser package defines auxiliary classes used by the javax. swing.JFileChooser component. You can customize the behavior of a JFileChooser by defining concrete subclasses of these abstract classes. Define a FileFilter to specify what files should be displayed by the JFileChooser. Define a FileView to specify how it should display those classes. And define a FileSystemView to specify how it should traverse the file system. Figure 27-1 shows the class hierarchy of this package. Figure 27-1. The javax.swing.filechooser package
This abstract class defines the methods used by the JFileChooser component to select a subset of files for display. When the accept() method is passed a File object, it should return true if that file should be displayed by the JFileChooser and false otherwise. accept() often makes its determination based on the filename extension, but it can also take other factors into account, such as the readability and writability of the file. The getDescription() method must return a human-readable description (preferably localized for the current locale) of the filtering operation performed by the filter. For example, if a FileFilter accepts files that end with extensions .gif, .jpg, and .png, the getDescription() method might return the string "Image files". Once you have created a FileFilter, you can tell a JFileChooser to use it by passing the filter to the setFileFilter() method. If you want to allow the user to choose among a set of file filters, pass the FileFilter objects to the addChooseableFileFilter() method of JFileChooser. Do not confuse javax.swing.filechooser.FileFilter with its less-powerful relative, java.io.FilenameFilter.
Passed To: JFileChooser.{addChoosableFileFilter(), removeChoosableFileFilter(), setFileFilter()} Returned By: JFileChooser.{getAcceptAllFileFilter(), getChoosableFileFilters(), getFileFilter()}, javax.swing.plaf.FileChooserUI.getAcceptAllFileFilter()
FileSystemView abstracts the system dependencies of the native filesystem and provides a platform-independent view of the filesystem for the JFileChooser component. As of Java 1.2, the features of FileSystemView are provided directly by the java.io.File class, so this class exists for portability to Java 1.1 systems. You can obtain the FileSystemView object for the current platform by calling the static getFileSystemView() method. The getRoots() method returns a list of root directories for the system. For Unix systems, there is only one, the / directory. On Windows systems, however, there is a root directory for each active drive letter. Use getHomeDirectory() to obtain a user's home directory on systems that have such a concept. Use isHiddenFile() to determine if a file is a hidden file according to the conventions of the native platform. Use getFiles() to list the contents of a directory, optionally omitting hidden files.
Passed To: JFileChooser.{JFileChooser(), setFileSystemView(), setup()} Returned By: JFileChooser.getFileSystemView(), FileSystemView.getFileSystemView()
This abstract class defines methods that the JFileChooser component uses to obtain information about how it should display a file. JFileChooser uses a FileView object provided by the current look-and-feel implementation to provide such things as the default icons for files and directories. You can implement your own FileView object to override some or all of the default behavior of the FileView provided by a look-and-feel. To do so, pass your FileView to the setFileView() method of the JFileChooser. The JFileChooser always queries your FileView object first, but when any of your methods return null, it calls the corresponding method of the look-and-feel FileView. By far, the most common reason to create a custom FileView class is to implement the getIcon() method to return custom icons for specific types of files. The other methods of FileView are not so commonly used.
Passed To: JFileChooser.setFileView() Returned By: JFileChooser.getFileView(), javax.swing.plaf.FileChooserUI.getFileView() Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|