Introduction
File filters are an easy check to see if a file meets some specific criteria. Generally, these are used to limit the available list of files to open when the user is shown a file chooser dialog (JFileChooser). While there is no limit to see if a file is to be displayed (it could even be random), the most common kind of filter is to filter by the file extension. In this tip, I'll go through the basics of creating your own file filters and how to use them with your JFileChooser.
Difficulty: Easy-Medium. I'm assuming you have some experience with the Java language syntax, but extensive knowledge is not necessary. I will only discuss portions of JFileChooser that pertain to the file filters feature.
Creating the File Filter
To create your own file filter, all you need to do is extend the FileFilter class and implement the two abstract methods accept() and getDescription().
public class TextFileFilter extends FileFilter { public boolean accept(File f) { if(f.isDirectory()) { return true; } return f.getName().endsWith(".txt"); } public String getDescription() { return "Text files (*.txt)"; } }
The accept() method
This method takes in a file, then using whatever logic you want to put in, determines if the file is to be displayed in the JFileChooser file list. Generally, you'll want to keep the check fairly simple because this method is called on every file in the current directory to see if it's going to be displayed in the list or not.
Note: The file passed is not necessarily guaranteed to be an actual file, it could be a directory. If you want the user to be able to see the directory so they can navigate through them, you must return true if the file is indeed a directory.
The getDescription() method
This method is called by the JFileChooser to get the description of the file filter that is currently being used. Basically you can return whatever string you want. Note that while you can return empty or even null strings, it is strongly advised that you don't.
Using the filters
To use the file filter, you must first instantiate it and then add it to the list of choosable file filters for your JFileChooser object. If you want the JFileChooser to have a certain file filter active, use the setFileFilter() method (note: this method will add the file filter to the choosable file filters list if it's not already there).
JFileChooser chooser = new JFileChooser(); chooser.addChoosableFileFilter(new TextFileFilter()); chooser.setFileFilter(new RandomFileFilter());
The above code will add the text file filter first in the list, then it will add the rich text file filter and set that as the active filter.
The "accept all" file filter
There's a default file filter that accepts all files and directories. You can enable/disable choosability of this filter using the setAcceptAllFileFilterUsed() method. You can also get the filter if you wish to set the accept all file filter as the filter you want to use.
chooser.setAcceptAllFileFilterUsed(true); // we can select the accept all file filter chooser.setFileFilter(chooser.getAcceptAllFileFilter()); // set the active file filter to teh accept all file filter
Removing choosable file filters
If you decide you don't want the user to be able to use a certain file filter anymore, you can remove it using the removeChoosableFileFilter() method.
chooser.removeChoosableFileFilter(myFilter); // returns true if myFilter was removed, false otherwise
Conclusion
For more information on JFileChooser and FileFilters, see How to Use File Choosers