public class ExtensionFilenameFilter extends PtFilenameFilter
Extensions are of the type ".foo", which is typically found on Windows and Unix boxes, but not on Macintosh. Case is ignored.
Extension - create a new filter that filters out all files but gif and jpg image files:
JFileChooser chooser = new JFileChooser(); ExtensionFilenameFilter filter = new ExtensionFilenameFilter( new String{"gif", "jpg"}, "JPEG & GIF Images") chooser.addChoosableFileFilter(filter); chooser.showOpenDialog(this);
Note that as of Java 1.6, there is a FileNameExtensionFilter which replaces this class. See http://download.oracle.com/javase/6/docs/api/javax/swing/filechooser/FileNameExtensionFilter.html However, this class can be used with both java.awt.FileDialog and javax.swing.JFileChooser because it implements java.io.FilenameFilter and extends javax.swing.FilenameFilter.
Constructor and Description |
---|
ExtensionFilenameFilter()
Creates a file filter.
|
ExtensionFilenameFilter(java.util.List<java.lang.String> extensions)
Construct a file filter that filters out all files that do
not have one of the extensions in the given list.
|
ExtensionFilenameFilter(java.lang.String extension)
Creates a file filter that accepts files with the given extension.
|
ExtensionFilenameFilter(java.lang.String[] filters)
Creates a file filter from the given string array.
|
ExtensionFilenameFilter(java.lang.String[] filters,
java.lang.String description)
Creates a file filter from the given string array and description.
|
ExtensionFilenameFilter(java.lang.String extension,
java.lang.String description)
Creates a file filter that accepts the given file type.
|
Modifier and Type | Method and Description |
---|---|
boolean |
accept(java.io.File f)
Return true if this file should be shown in the directory pane,
false if it shouldn't.
|
boolean |
accept(java.io.File directory,
java.lang.String name)
Return true if this file should be shown in the directory pane,
false if it shouldn't.
|
void |
addExtension(java.lang.String extension)
Add a filetype "dot" extension to filter against.
|
java.lang.String |
getDefaultExtension()
If the filter contains only one extension, return the extension
name.
|
java.lang.String |
getDescription()
Return the human readable description of this filter.
|
java.lang.String |
getExtension(java.io.File f)
Return the extension portion of the file's name.
|
boolean |
isExtensionListInDescription()
Return true if the extension list (.jpg, .gif, etc) should
show up in the human readable description.
|
void |
setDescription(java.lang.String description)
Set the human readable description of this filter.
|
void |
setExtensionListInDescription(boolean useExtensionListInDescription)
Determine whether the extension list (.jpg, .gif, etc) should
show up in the human readable description.
|
java.lang.String |
toString()
Return a string description of this filter.
|
public ExtensionFilenameFilter()
addExtension(String)
public ExtensionFilenameFilter(java.lang.String extension)
extension
- The file name extension with no leading period
('.').addExtension(String)
public ExtensionFilenameFilter(java.lang.String extension, java.lang.String description)
extension
- The file name extension with no leading period
('.').description
- A description of this file filter.addExtension(String)
public ExtensionFilenameFilter(java.lang.String[] filters)
filters
- An array of file extensions.addExtension(String)
public ExtensionFilenameFilter(java.lang.String[] filters, java.lang.String description)
filters
- An array of file extensions.description
- A description of this file filter.addExtension(String)
public ExtensionFilenameFilter(java.util.List<java.lang.String> extensions)
extensions
- A list of extensions, each of which is
a String.public boolean accept(java.io.File f)
Files that begin with "." are ignored.
This method is used by javax.swing.JFileChoosers.
accept
in class PtFilenameFilter
f
- The file to be checked.accept(File, String)
,
getExtension(File)
,
FileFilter.accept(File)
public boolean accept(java.io.File directory, java.lang.String name)
Files that begin with "." are ignored.
This method is used by java.awt.FileDialog.
accept
in interface java.io.FilenameFilter
accept
in class PtFilenameFilter
directory
- The directory in which the file was found.name
- The name of the file.accept(File)
,
getExtension(File)
,
FileFilter.accept(File)
public void addExtension(java.lang.String extension)
ExtensionFileFilter filter = new ExtensionFileFilter(); filter.addExtension("jpg"); filter.addExtension("tif");Note that the "." before the extension is not needed and will be ignored.
extension
- The extension to be added.public java.lang.String getDefaultExtension()
public java.lang.String getDescription()
getDescription
in class PtFilenameFilter
setDescription(String)
,
setExtensionListInDescription(boolean)
,
isExtensionListInDescription()
,
FileFilter.getDescription()
public java.lang.String getExtension(java.io.File f)
f
- The file.public boolean isExtensionListInDescription()
getDescription()
public void setDescription(java.lang.String description)
description
- The human readable description of this
filter.getDescription()
,
setExtensionListInDescription(boolean)
,
isExtensionListInDescription()
public void setExtensionListInDescription(boolean useExtensionListInDescription)
useExtensionListInDescription
- True if the list of extensions
should appear in the human readable description.getDescription()
,
isExtensionListInDescription()
public java.lang.String toString()
toString
in class java.lang.Object
getDescription()