Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JFileChooser – Implement Custom FileFilter

1 About FileFilter

JFileChooser gives a class called FileNameExtensionFilter which is the implementation of FileFilter abstract to deal with the file name extension. Using this class, one can add file filters to the JFileChooser.

In our example, we will derive from the FileFilter and create File Filters for the Lib and Jar files. If required, one can add specialized validation so that JFileChooser will show the files only when such a condition is met. For example, we can only show the lib file when the lib file has the prefix of dab_ and its size exceeds 1 MB.

2. JFileChooser & FileFilter

Now have a look at the below picture:

JFileFilter and JFileChooser
JFileFilter and JFileChooser

The FileFilter class can be used to create our own file filters. We will need to override the accept and getDescription methods in the extended class. The accept method determines which files are displayed and which are skipped by the JFileChooser. The file extension description is displayed in the file type combo box via the method getDescription.

Once custom FileFilter is created, we can pass that to the JFileChooser via the method addChoosableFileFilter. Note, the JFileChooser accepts multiple file filters and in our case, we will add file filters for Lib and Jar files.

Youtube: About FileFilter & JFileChooser

3. About JFileChooser – File Filter Example

The below screen shows the example we want to create here:

The JFileChooser dialog shows three filters when the user clicks the drop-down button. In this example, we will add two filters for the JFileChooser to show only Jar files or Lib files. All Files filter will show all the files by default. Here, in our example, all three filters shows directories.

Youtube – About the Example

4. FileFilter – Lib File Filter

The FileFilter abstract in Swing expects to implement two methods named accept and getDescription. The way we carry out the accept method decides what file needs to be shown in the JFileChooser dialog.

4.1 The accept Method Override

The accept method will return true or false. When we implement, we should consider this, and we will return true after checking the file extension and it is matching to a specific one say lib file. Note, one can also add other specification like the file size should be in specific range. Below code shows the accept method override for the Lib File Filter:

FileFilter Abstract - accept Override
FileFilter Abstract – accept Override
  1. Here, we allow the directories. The isDirectory method of the File class returns true when the instance is representing a directory.
  2. It indicates that the file does not have any extension at all. So, we do not consider such a file as a Lib file and hence return false. Means, we do not allow the files without extensions. Swing’s JFileChooser now does not list such a file when user picked the Lib file filter.
  3. Finally, here we check the extension is “Lib” and return true asking the JFileChooser to show file in the UI.

4.2 Override getDescription

When we override the getDescription method, we return the string, which will be displayed in the File Type filter combo box. We return a string description for the lib file filter in the below code:

5. FileFilter – Jar File Filter

We can do the same thing with the Jar File filter as well.

You can see the override of the getDescription abstract method in the code below for the Jar File Filter . At line number 5, we return the description which will be displayed on the Files of Type combo box.

Here is the code for the accept() method. We implement this method the same way we did for the Lib File Filter.

6. Prepare JFrame Window

Our JFrame contains only one button labelled, Test File Chooser. FlowLayout manager is managing the layout for the Frame window. At line 4, we created the JFileChooser, keeping it as a class member for later use. Below is the code:

7. Set the File Filters for JFileChooser

The method addChoosableFileFilter accepts a FileFilter, and we set both of our file filters to the JFileChooser dialog component. After the method call, JFileChooser will have the capability of filtering the file by its extension’s lib or jar. Note, by default Java Swing applies All Files filter to the JFileChooser.

The method setAcceptAllFileFilterUsed with a boolean parameter false will cut-down the ‘All Files’ filter. This means, the user can only select Lib or Jar files. In our example, we have not used it.

8. Display JFileChooser With JFileFilter

Anonymous handlers function for the JButton’s ActionEvent launches the JFileChooser. Since we already set the File-Filters, the dialog will now show the Lib and Jar file filters. The user can now filter only the Lib Files or Jar Files or all the files by using the default filter.

Youtube : Code Implementation and Test Run

9. Code Reference

9.1 MainEntry.java

9.2 LibFileFilter.java

9.3 JarFileFilter.java

9.4 JFileChooserFileFilterExample.java

Categories: Swing

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.