Book Home Java Enterprise in a Nutshell Search this book

Chapter 27. The javax.swing.filechooser Package

The 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

Figure 27-1. The javax.swing.filechooser package

FileFilterJava 1.2
javax.swing.filechooser

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.

public abstract class FileFilter {
// Public Constructors
public FileFilter ();
// Public Instance Methods
public abstract boolean accept (java.io.File f);
public abstract String getDescription ();
}

Passed To: JFileChooser.{addChoosableFileFilter(), removeChoosableFileFilter(), setFileFilter()}

Returned By: JFileChooser.{getAcceptAllFileFilter(), getChoosableFileFilters(), getFileFilter()}, javax.swing.plaf.FileChooserUI.getAcceptAllFileFilter()

FileSystemViewJava 1.2
javax.swing.filechooser

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.

public abstract class FileSystemView {
// Public Constructors
public FileSystemView ();
// Public Class Methods
public static FileSystemView getFileSystemView ();
// Property Accessor Methods (by property name)
public java.io.File getHomeDirectory ();
public abstract java.io.File[ ] getRoots ();
// Public Instance Methods
public java.io.File createFileObject (String path);
public java.io.File createFileObject (java.io.File dir, String filename);
public abstract java.io.File createNewFolder (java.io.File containingDir) throws java.io.IOException;
public java.io.File[ ] getFiles (java.io.File dir, boolean useFileHiding);
public java.io.File getParentDirectory (java.io.File dir);
public abstract boolean isHiddenFile (java.io.File f);
public abstract boolean isRoot (java.io.File f);
}

Passed To: JFileChooser.{JFileChooser(), setFileSystemView(), setup()}

Returned By: JFileChooser.getFileSystemView(), FileSystemView.getFileSystemView()

FileViewJava 1.2
javax.swing.filechooser

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.

public abstract class FileView {
// Public Constructors
public FileView ();
// Public Instance Methods
public abstract String getDescription (java.io.File f);
public abstract Icon getIcon (java.io.File f);
public abstract String getName (java.io.File f);
public abstract String getTypeDescription (java.io.File f);
public abstract Boolean isTraversable (java.io.File f);
}

Passed To: JFileChooser.setFileView()

Returned By: JFileChooser.getFileView(), javax.swing.plaf.FileChooserUI.getFileView()



Library Navigation Links

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