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:
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:

- Here, we allow the directories. The isDirectory method of the File class returns true when the instance is representing a directory.
- 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.
- 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:
1 2 3 4 5 6 7 |
//Sample 2.2: Implement Description @Override public String getDescription() { //Sample 09: Java Populates the File Choose Entry //with this method Return Value return "Java Library Files (*.Lib)"; } |
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.
1 2 3 4 5 6 |
//Sample 3.2: Implement Description @Override public String getDescription() { //Sample 12: Desc for Jar File return "Java jar Files(*.Jar)"; } |
Here is the code for the accept() method. We implement this method the same way we did for the Lib File Filter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//Sample 3.1: Implement Accept @Override public boolean accept(File f) { //Sample 13: Accept For Jar File Filter if (f.isDirectory()) return true; String FileName = f.getName(); int ExtensionStart = FileName.lastIndexOf("."); int ExtensionEnd = FileName.length(); if (ExtensionStart < 0) return false; String Extension; Extension = FileName.substring(ExtensionStart, ExtensionEnd); if (Extension.equalsIgnoreCase(".Jar") == true) return true; else return false; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class JFileChooserFileFilterExample extends JFrame { //Sample 05: Create File Chooser JFileChooser jfc = new JFileChooser(); public JFileChooserFileFilterExample(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 300, 100); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 04: Create a Button and Add it JButton btnTest = new JButton("Test File Chooser"); ControlHost.add(btnTest); |
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.
1 2 3 |
//Sample 06: Set the Filter jfc.addChoosableFileFilter(new LibFileFilter()); jfc.addChoosableFileFilter(new JarFileFilter()); |
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.
1 2 3 4 5 6 7 8 |
//Sample 07: Implement the Btn Click btnTest.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //Sample 08: Show the Dialog and Test Filters jfc.showOpenDialog(JFileChooserFileFilterExample.this); } }); |
Youtube : Code Implementation and Test Run
9. Code Reference
9.1 MainEntry.java
1 2 3 4 5 6 7 8 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 06: Create Instance of JFrameDemo JFrameDemo frame = new JFrameDemo("File Filter Example"); frame.setVisible(true); } } |
9.2 LibFileFilter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package tube.coding.examples; import java.io.File; import javax.swing.filechooser.FileFilter; //Sample 02: Extend from //javax.swing.filechooser.FileFilter Abstract //Called By File Chooser Dialog public class LibFileFilter extends FileFilter { //Sample 2.1: Implement Accept @Override public boolean accept(File f) { //Sample 10: Accept Directories if (f.isDirectory()) return true; //Sample 11: Accept only .Lib Files String FileName = f.getName(); int ExtensionStart = FileName.lastIndexOf("."); int ExtensionEnd = FileName.length(); //No Extension if (ExtensionStart < 0) return false; String Extension; Extension= FileName.substring(ExtensionStart, ExtensionEnd); if (Extension.equalsIgnoreCase(".Lib") == true) return true; else return false; } //Sample 2.2: Implement Description @Override public String getDescription() { //Sample 09: Java Populates the File Choose Entry //with this method Return Value return "Java Library Files (*.Lib)"; } } |
9.3 JarFileFilter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package tube.coding.examples; import java.io.File; import javax.swing.filechooser.FileFilter; //Sample 03: Extend from //javax.swing.filechooser.FileFilter Abstract //Called By File Chooser Dialog public class JarFileFilter extends FileFilter { //Sample 3.1: Implement Accept @Override public boolean accept(File f) { //Sample 13: Accept For Jar File Filter if (f.isDirectory()) return true; String FileName = f.getName(); int ExtensionStart = FileName.lastIndexOf("."); int ExtensionEnd = FileName.length(); if (ExtensionStart < 0) return false; String Extension; Extension = FileName.substring(ExtensionStart, ExtensionEnd); if (Extension.equalsIgnoreCase(".Jar") == true) return true; else return false; } //Sample 3.2: Implement Description @Override public String getDescription() { //Sample 12: Desc for Jar File return "Java jar Files(*.Jar)"; } } |
9.4 JFileChooserFileFilterExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package tube.coding.examples; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class JFileChooserFileFilterExample extends JFrame { //Sample 05: Create File Chooser JFileChooser jfc = new JFileChooser(); public JFileChooserFileFilterExample(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 300, 100); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 04: Create a Button and Add it JButton btnTest = new JButton("Test File Chooser"); ControlHost.add(btnTest); //Sample 06: Set the Filter jfc.addChoosableFileFilter(new LibFileFilter()); jfc.addChoosableFileFilter(new JarFileFilter()); //Sample 07: Implement the Btn Click btnTest.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //Sample 08: Show the Dialog and Test Filters jfc.showOpenDialog(JFileChooserFileFilterExample.this); } }); } } |
Categories: Swing
Tags: accept(), addChoosableFileFilter(), FileFilter, getDescription(), setAcceptAllFileFilterUsed()