JClass Elements

PreviousNextIndex

19

JCFileFilter

Features of JCFileFilter  Constructors  Methods  Example 


19.1 Features of JCFileFilter

JCFileFilter enhances Swing's JFileChooser by allowing you to pass it file extensions. These are the only ones that appear in the file chooser dialog. Extensions are of the type ".txt," ".java," and so on, which are typically found on Windows and Unix platforms, but not on the Macintosh. Case is ignored, so ".txt" is equivalent to ".TXT" as far as the filter is concerned.

The class has versatile constructors and convenience methods for setting both the extensions that are to be filtered and an optional descriptive phrase.


19.2 Constructors

JCFileFilter has four constructors:

JCFileFilter()

The default constructor. This form of the constructor is used to list all file types. To filter files, use the addExtension() method. To provide a human-readable description for the extension, use the setDescription() method.

JCFileFilter(String   extension)

Creates a file filter that, initially at least, shows only files of the type whose extension is specified by the String argument. The period (dot) before the extension is optional. More filename extensions may be added using the addExtension() method.

JCFileFilter(String[]   filters)

Creates a file filter for a list of file extensions given as a String array. More may be added using the addExtension() method.

JCFileFilter(String   extension, String   description)

Creates a file filter that, initially at least, shows only files of the type whose extension is specified by the String argument. The period (dot) before the extension is optional. More filename extensions may be added using the addExtension() method.

The human-readable description is retrieved using the getDescription() method.


19.3 Methods

These methods let you set or examine the elements of the filtering process:

addExtension(String   extension)

Places an additional filename extension in the list of those to filter against. Any filename matching this extension is listed in the file dialog, as well as those that have been added in the constructor. The method may be used multiple times to form a list of extensions to filter against.

accept(File f)

This Boolean method is used to determine if the given filename has an extension that is in the list of those to be filtered, that is, to be displayed in the file dialog.

Files that begin with "." are ignored, but directories are always shown.

getExtension(Strings)

Returns the extension portion of a String.

set/getDescription()

Sets or gets the human readable description of this filter. This String is used to preface the list of file extensions that shows up in the file dialog's "Files of type:" combo box.

 

For example, if the filter is set to:

String[] filterTypes = {"gif", "jpg"};

and the description is specified as "JPEG & GIF Images", then the combo box displays "JPEG & GIF Images (*.gif, *.jpg)"

setExtensionListIn
  Description(

    boolean b)

isExtensionListIn
  Description()

Determines whether the extension list, for example (*.jpg, *.gif), should show up in the human-readable description.

The corresponding Boolean method returns the policy currently in effect.


19.4 Example

The following code snippet sets up a filter for GIF and Java source files. The description, which appears in the file chooser's "Files of type:" combo box along with the extensions themselves, is provided through a parameter passed to the constructor.

Note that you can control whether either part of the description actually appears through the use of setDescription() and setExtensionListInDescription().

JFileChooser chooser = new JFileChooser();
String[] filterTypes = {"gif", "java"};
JCFileFilter filter = new JCFileFilter(filterTypes, "GIF Images and Java source files");
chooser.addChoosableFileFilter(filter);

Figure 51 :  A JCFileFilter for Java source files and GIF images.


PreviousNextIndex