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 27-1. The javax.swing.filechooser package

FileFilterJava 1.2

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,

public abstract class FileFilter {
// Public Constructors
public FileFilter ();
// Public Instance Methods
public abstract boolean accept ( 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

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 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 getHomeDirectory ();
public abstract[ ] getRoots ();
// Public Instance Methods
public createFileObject (String path);
public createFileObject ( dir, String filename);
public abstract createNewFolder ( containingDir) throws;
public[ ] getFiles ( dir, boolean useFileHiding);
public getParentDirectory ( dir);
public abstract boolean isHiddenFile ( f);
public abstract boolean isRoot ( f);

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

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

FileViewJava 1.2

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 ( f);
public abstract Icon getIcon ( f);
public abstract String getName ( f);
public abstract String getTypeDescription ( f);
public abstract Boolean isTraversable ( 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.