JFileChooser UI Tutorial


Summary:

A File Chooser is a GUI element that allows the user to navigate through a filesystem to select a specific file or directory. Common places these are found are when selecting a directory to install software to, creating new files, opening existing files, or saving files. The JFileChooser is a quick and easy way to add File Chooser functionality to your GUI based application. The JFileChooser provides a basic interface that allows for filesystem navigation. It also allows for directory and file selection. Some additional features include directory creation and file filtering. The JFileChooser provides both basic open and save dialogs, as well as a way for customizing the File Chooser dialog for specific uses. Although this GUI element may not solve advanced file selection problems, it provides a thorough enough functionality for most general purpose needs.


Where to Find It:

The JFileChooser is part of Swing and can be found at javax.swing.JFileChooser

The Basics:

Using a JFileChooser in it's most basic form is a simple task. Simple instantiate the JFileChooser using the desired starting directory. Then you can either show the JFileChooser as an open or save dialog by calling showOpenDialog() or showSaveDialog() respectively. Both of these methods return a int value indicating the outcome of the file selection process. If a file was successfully selected, you can then call getSelectedFile() on the JFileChooser to retrieve the file(s) that was selected. A simple example of this is shown in the code below.

JFileChooser fChoose = new JFileChooser( 
	System.getProperty( "user.dir" ) );
int returnVal = fChoose.showOpenDialog( parentFrame );

if ( returnVal == JFileChooser.APPROVE_OPTION ) {
	
	System.out.println( "The file selected was: " + 
		fChoose.getSelectedFile().getName() );
}
			

Customization:

There are several features of the JFileChooser that allow for customization. One of the most useful features is the ability to set and arrange file filters. A FileFilter is an object that filters out the files that are displayed based upon some programmed criteria. The code below shows how to filter out for .txt files by simply checking the extension content.

class TextFileFilter extends FileFilter {
	
	public boolean accept( File f ) {
		
		boolean retVal = false;
		
		String ext = f.getName().substring( f.getName().length() - 3 );
		
		if ( ext.equalsIgnoreCase( "txt" ) || f.isDirectory() )
			retVal = true;

		return retVal;
	}
	
	public String getDescription() {
		
		return "Text Files ( *.txt )";
	}
} // TextFileFilter
			
To use this FileFilter, you could make the following call on the JFileChooser. In the example below, the original filter that filters for all files is removed and then placed at the end. The TextFileFilter is set to be selected by default.
FileFilter all = fChoose.getAcceptAllFileFilter();
fChoose.removeChoosableFileFilter( all );
FileFilter startFilter = new TextFileFilter();
fChoose.addChoosableFileFilter( startFilter );
fChoose.addChoosableFileFilter( all );
fChoose.setFileFilter( startFilter );
			
Some other areas for customization include specifying whether only files, directories, or both are selectable. This can be set using the setFileSelectionMode( int ), with either JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY, or JFileChooser.FILES_AND_DIRECTORIES. Additionally, you can support selecting multiple files with the setMultiSelectionEnabled(boolean b) method. If using multiselect, you can retrieve the files later by calling getSelectedFiles() which returns a File[]. Instead of using showOpenDialog or showSaveDialog, a custom dialog can be displayed. By calling showDialog(parent, approveText) you can create a dialog that uses the approveText as the text on the approve button, instead of save or open. One of the more advanced features is the ability to set a FileSystemView object. This object allows for customizing the structuring of the file system display to better fit the operating system. While this can be handy, if you need this level of customization, you might considercreating your own FileChooser from scratch.


Best Practices:

When using the JFileChooser, keep in mind that it is designed to provide a quick and easy way to allow for graphical file selection. If you find yourself trying to customize the selection process extensively, it is most likely the case that the JFileChoose is not the UI Component you should be using. When making file filters, keep the number of file types in each filter to one or two, to prevent user confusion. Also, make sure you add clear and concise descriptions so the user knows exactly what type of files they are filtering for. Also, don't hesitate to use the showDialog(parent, approveText) method to show a dialog that may be more relevant to the task you are trying to accomplish.


Limitations:

The JFileChooser is decent at quickly allowing for the creation of a graphical file selection system. However, due to this out of the can approach, it is not suited for many scenarios. Things like showing file thumbnails, displaying template files for new file creation, and customizing the actual look of the dialog are not easily accomplished, if they can be accomplished at all. The JFileChooser is well suited for providing a standard solution for basic file opening and saving, but outside of that, it would be highly recommended that a component is developed from scratch.

Things to Watch Out for When Including JFileChooser:

There are several things that should be watched for when using the JFileChooser. When creating a chooser, be sure the set the initial directory to something that is useful to the user. There is nothing more frustrating than opening a save dialog that defaults to the system root. Also, when creating file filters, be sure to keep the all filter as backup as that is a common freedom given to the user in almost all file choosers.

References: