Search This Blog

How to Filter Filetypes using File Extensions in JFileChooser - Java File Chooser

how to filter files in Jfilechooser by file types or file formats using extensions in filename java codefile name file type filefilter file filter filtering files
Screenshot of following example java program
In java, to browse files or folders from the file system, we use the JFileChooser class. You can use  the JFileChooser object in two modes, save mode or open mode. Here we discuss about filtering file type by file extensions for opening the file. By default, the JFileChooser object will have an AcceptAllFileFilter. This file filter will allow the user to select file of any file type. The file filter will display text All files. If you want to restrict the file chooser to open only one type of file, you should first remove the AcceptAllFileFilter. To do this, call the following method:

myfilechooser.removeChoosableFileFilter(createoropenchooser.getAcceptAllFileFilter());



Then, you may add other FileFilters. A file filter (javax.swing.filechooser.FileFilter) is a java class which is used to filter only a group of type of files. If you want to show only image files in a JFileChooser window, you should first create a new FileFilter object as follows:

FileFilter ff=new FileNameExtensionFilter("Image files", "jpg","gif","png","bmp");

You should import
javax.swing.filechooser.FileNameExtensionFilter
for this. The above file filter will filter and show only image files of given extensions ("jpg","gif","png" and "bmp"). In the filetype selection drop-down list in file-chooser, you can see the text Image files. The following java program will describe you how to use FileFilters in java JFIleChooser objects.

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class FileSelection
{

public static void main(String args[])
{
JFileChooser fc=new JFileChooser();
//Removing 'All Files(*.*)' filter. This is needed only if you dont want to allow to open files of all extensions 
fc.removeChoosableFileFilter(fc.getAcceptAllFileFilter());
FileFilter ff=new FileNameExtensionFilter("Image files", "jpg","bmp","png","gif","tiff","jpeg","tif");
fc.addChoosableFileFilter(ff);//ff added to filechooser
fc.setFileFilter(ff);//st ff as default selection
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);//user must select a file not folder
fc.setMultiSelectionEnabled(false);//disabled selection of multiple files
fc.showOpenDialog(null);
System.out.println("selected file:"+fc.getSelectedFile());
}

}

In the twelfth line, we removed the file filter that allows selection of all files. The getAcceptAllFileFilter() method return the filefilter object that allows selection of any kind of files. The removeChoosableFileFilter() method removes the specified filefilter. In 13th line we created a FileFilter object which consists of all popular image (picture) file formats. In the next line of code we added this file filter to our file chooser. In 15th line we set ff  as the default selection for file filter. Next line is optional which prevents selection of folders. The 17th line l=disbales selection of multiple files in the jfilechooser object. This is also optional. In case you want to choose multiple files in your java program, you should avoid this line. 18th line sets the JFileChooser object fc visible. You can pass any component to make the filechooser modal. You may pass null to make it modeless. To know more about modal and modeless dialogues, see this post: What Does Modal and Modeless (Non Modal) Mean in Java.

The showOpenDialog() method is a blocking method. The java code execution jumps to next line only after you dismiss the filechooser window by either choosing a file or clicking 'cancel'. At last we displayed the selected file. The method fc.getSelectedFile() returns the selected file as an object of  File class. If you use fc.setMultiSelectionEnabled(true) instead of 17th line, you can choose multiple files. Then to get the selected (multiple) files, you should call fc.getSelectedFiles() instead of fc.getSelectedFile(). fc.getSelectedFiles() will return an array of File (File[]).


No comments:

Post a Comment