Java Reference
In-Depth Information
Using the file filter entails creating it and associating it with the JFileChooser . If you just
want to make the filter selectable by the user, but not the default initial selection, call public
void addChoosableFileFilter(FileFilter filter) . This will keep the default accept-all-files
filter selected. If, instead, you want the filter to be set when the chooser first appears, call
public void setFileFilter(FileFilter filter) , and the file chooser will filter the initial set of
files shown.
For example, the following source will add two filters to a file chooser:
FileFilter jpegFilter =
new ExtensionFileFilter(null, new String[]{ "JPG", "JPEG"} );
fileChooser.addChoosableFileFilter(jpegFilter);
FileFilter gifFilter = new ExtensionFileFilter("gif", new String[]{ "gif"} );
fileChooser.addChoosableFileFilter(gifFilter);
When no file filters have been associated with the JFileChooser , the filter from
JFileChooser.getAcceptAllFileFilter() is used to provide a filter that accepts all files
and that is also appropriate for the underlying operating system.
Figure 9-27 shows an open filter selection combo box in a Motif file chooser.
Figure 9-27. Using custom FileFilter with a JFileChooser
 
Search WWH ::




Custom Search