Last Updated on 04 July 2019   |   Print Email
In this post, I will guide you how to add file filter for JFileChooser component in Java Swing applications.You know, it’s very common to have a file extension filter for open/save dialog like the following screenshot:
In Swing, we can do that by using methodaddChoosableFileFilter(FileFilter filter) of the class JFileChooser. Create a class that extends FileFilterabstract class and overrides its two methods:
boolean accept(File f): returns true if the file f satisfies a filter condition. The condition here is the extension of the file.
String getDescription(): returns a description which is displayed in the dialog’s Files of type section.
The following code shows an example of adding a filter for files of type PDF:
JFileChooser fileChooser = new JFileChooser(); fileChooser.addChoosableFileFilter(new FileFilter() {
public String getDescription() {
return "PDF Documents (*.pdf)";
}
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
} else {
return f.getName().toLowerCase().endsWith(".pdf");
}
}
});
However, what if we need to add several filters, such as for .docx, .xlsx files? Well, one solution is repeating the above code for each file type – but that is not good in terms of code reusability. So, it’s better to create a separate, generalized class for extending the FileFilterabstract class, and parameterize the file extension and description, as shown in the following code:
import java.io.File;
import javax.swing.filechooser.FileFilter;
public class FileTypeFilter extends FileFilter {
private String extension;
private String description;
public FileTypeFilter(String extension, String description) {
this.extension = extension;
this.description = description;
}
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
return file.getName().endsWith(extension);
}
public String getDescription() {
return description + String.format(" (*%s)", extension);
}
}
Then we can add several file filters as following:
FileFilter docFilter = new FileTypeFilter(".docx", "Microsoft Word Documents");
FileFilter pdfFilter = new FileTypeFilter(".pdf", "PDF Documents");
FileFilter xlsFilter = new FileTypeFilter(".xlsx", "Microsoft Excel Documents");
fileChooser.addChoosableFileFilter(docFilter);
fileChooser.addChoosableFileFilter(pdfFilter);
fileChooser.addChoosableFileFilter(xlsFilter);
Before Java 6, we have to write the above code manually. Fortunately, since Java 6, Swing adds a new class called FileNameExtensionFilter, which makes adding file filters easier. For example:
And following is a sample program that shows an open dialog when the button Browse is clicked, with some filters for: PDF documents, MS Office documents, and Images.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments
u do:
fileChooser.setAcceptAllFileFilterUsed(false);