Screenshot of following example java program |
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.FileNameExtensionFilterfor 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: .
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