public class FileFilter extends Object
FileChooser
.
The most typical usage is to only show the file types the application can deal with. On "Open" Dialogs, a FileFilter can be used to only shown the files the application can open. On the other side, on "Save as" Dialogs they can be used as a way to let users choose the format in which to save the document.
You will usually add
several
FileFilters to your FileChooser Widget. The FileChooser will show a
ComboBox with the different Filters, that the user can use to choose a
specific file type. You should use the setName()
method to set the name used as the label for the FileFilter on the
ComboBox.
In a FileFilter, you specify the files to shown either as with its MIME
type, with the addMimeType()
method, or as a
shell style glob pattern, with the addPattern()
method. You can add several MIME types or patterns to a single FileFilter.
This is useful, for example, to provide a "All Supported Files" filter.
As an example, consider the following code:
FileChooserDialog openDialog; FileFilter pngImages, jpegImages, allImages; openDialog = new FileChooserDialog("Open...", w, FileChooserAction.OPEN); // you can specify a MIME type... pngImages = new FileFilter("Portable Network Graphics (PNG)"); pngImages.addMimeType("image/png"); // ...or a pattern jpegImages = new FileFilter("JPEG Image"); jpegImages.addPattern("*.jpg"); // of course you can use several patterns or MIME types per filter jpegImages.addPattern("*.jpeg"); // and you can use both MIME types and patterns in a single filter allImages = new FileFilter("All supported images"); allImages.addMimeType("image/png"); allImages.addPattern("*.jpg"); allImages.addPattern("*.jpeg"); // Finally, you should add each filter to the FileChooser openDialog.addFilter(allImages); openDialog.addFilter(pngImages); openDialog.addFilter(jpegImages);
Constructor and Description |
---|
FileFilter()
Create a new FileFilter
|
FileFilter(String name)
Create a new FileFilter with the given name.
|
Modifier and Type | Method and Description |
---|---|
void |
addMimeType(String mimeType)
Add a rule allowing a given MIME type to the filter.
|
void |
addPattern(String pattern)
Adds a rule allowing a shell style glob to the filter.
|
void |
setName(String name)
Sets the human-readable name of the filter; this is the String that
will be displayed in the FileChooser user interface if there is a
selectable list of filters.
|
public FileFilter()
public FileFilter(String name)
setName()
method.public void addMimeType(String mimeType)
Filtering by MIME types handles aliasing and subclassing of mime types;
e.g. a filter for "text/plain"
also matches a file with
MIME type "application/rtf"
, since
"application/rtf"
is a subclass of
"text/plain"
. Note that FileFilter allows wildcards for
the subtype of a MIME type, so you can e.g. filter for
"image/*"
.
public void addPattern(String pattern)
public void setName(String name)