public class FileExtensionFilter extends FileFilter
FileFilter
that filters files
based on their filename extensions.
Example: Create a new filter that filters out all files except those whose filenames end in '.gif' or '.jpg'.
JFileChooser chooser = new JFileChooser(); FileExtensionFilter filter = new FileExtensionFilter( new String[] {"gif", "jpg"}, "GIF & JPEG Images"); chooser.addChoosableFileFilter(filter); chooser.showOpenDialog(this);
Modifier and Type | Field and Description |
---|---|
private String |
description |
private Hashtable |
filters |
private String |
fullDescription |
private boolean |
useExtensionsInDescription |
Constructor and Description |
---|
FileExtensionFilter()
Construct a new
FileExtensionFilter . |
FileExtensionFilter(String extension)
Construct a new
FileExtensionFilter that accepts files with
the given extension. |
FileExtensionFilter(String[] extensions)
Construct a new
FileExtensionFilter that accepts the given
extensions. |
FileExtensionFilter(String[] extensions,
String description)
Construct a new
FileExtensionFilter that accepts the given
extensions. |
FileExtensionFilter(String extension,
String description)
Construct a new
FileExtensionFilter that accepts the given
file type. |
Modifier and Type | Method and Description |
---|---|
boolean |
accept(File f)
Filter a file.
|
void |
addExtension(String extension)
Adds an extension to filter against.
|
String |
getDescription()
Get the description of this filter
|
private String |
getExtension(File f)
Get the extension portion of a file's name.
|
boolean |
isExtensionListInDescription()
Determine whether the extension list will appear as part of the
description.
|
void |
setDescription(String description)
Set the description for this filter.
|
void |
setExtensionListInDescription(boolean flag)
Specify whether the extension list should appear as part of the
description.
|
private String description
private String fullDescription
private boolean useExtensionsInDescription
public FileExtensionFilter()
FileExtensionFilter
. If no extensions are
added to this filter, then all files will be accepted.public FileExtensionFilter(String extension)
FileExtensionFilter
that accepts files with
the given extension. For example:
new FileExtensionFilter("jpg");
extension
- The extension.public FileExtensionFilter(String extension, String description)
FileExtensionFilter
that accepts the given
file type. For example:
new FileExtensionFilter("jpg", "JPEG Image Images");
Note that the '.' is not part of the extension and should not be included.
extension
- The extension.description
- A description of the extension.public FileExtensionFilter(String[] extensions)
FileExtensionFilter
that accepts the given
extensions. For example:
new FileExtensionFilter(String {"gif", "jpg"});
Note that the '.' is not part of the extension and should not be included.
extensions
- An array of extensions.public FileExtensionFilter(String[] extensions, String description)
FileExtensionFilter
that accepts the given
extensions. For example:
new FileExtensionFilter(new String[] {"gif", "jpg"}, "Image Files");
Note that the '.' is not part of the extension and should not be included.
extensions
- An array of extensions.description
- A description for these extensions.public boolean accept(File f)
accept
in class FileFilter
f
- The File
to filter.true
if the file ends in one of the
extensions that this object is filtering, and false
if it does not or if it is a hidden file (a file whose name
begins with '.').private String getExtension(File f)
f
- The file.public void addExtension(String extension)
For example, the following code will create a filter that accepts only files whose names end with ".jpg" or ".gif":
FileExtensionFilter filter = new FileExtensionFilter();
filter.addExtension("jpg");
filter.addExtension("gif");
Note that the '.' is not part of the extension and should not be included.
extension
- The extension to add.public String getDescription()
getDescription
in class FileFilter
public void setDescription(String description)
description
- The new description.public void setExtensionListInDescription(boolean flag)
setDescription()
.flag
- A flag specifying whether or not the extensions should be
listed in the description.public boolean isExtensionListInDescription()
true
if the list will appear in the description, and
false
otherwise.WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses