1. Computing

Presenting a File Dialog to the User

By

One of the most common functions of a Java application is to allow a user to open or save a file. Typically a dialog box will pop up and show the user their file system. It enables them to navigate through the difference directories in the file system until they find the right location to either save or open a file. It's not necessary to build your own file dialog box from scratch because the Java API provides a class called JFileChooser to create one for you.

The JFileChooser Class

To use the JFileChooser class you need to first create an instance of the class:

JFileChooser fileDialog = new JFileChooser();

Now that you have a JFileChooser object at your disposal you can use it to create a file dialog whenever your application needs one.

Note: Unlike the dialogs created using the JOptionPane class the JFileChooser is created without a parent component specified. This means that if your application has multiple frames you can pick which one to use as the parent when you show the file dialog. The parent component determines where the file dialog appears on the screen

Choosing a File

When you need a file dialog to let the user pick a file from their file system call the showOpenDialog method:

//Using the JFrame as a parent for the file dialog box
JFrame guiFrame = new JFrame();
JFileChooser fileDialog = new JFileChooser();
int openChoice = fileDialog.showOpenDialog(guiFrame);

The showOpenDialog method returns an int value representing the choice the user has made. The choice are cancel button pressed (i.e., JFileChooser.CANCEL_OPTION), open buton pressed (i.e., JFileChooser.APPROVE_OPTION), and if the dialog is closed without either button being pressed (i.e., JFileChooser.ERROR_OPTION):

if (openChoice == JFileChooser.APPROVE_OPTION)
{
    File selectedFile = fileDialog.getSelectedFile();
}

Then a File object can be used to retrieve the selected file using the getSelectedFile method.

Saving a File

To show a file dialog to enable a user to save a file to their file system call the showSaveDialog method:

//Using the JFrame as a parent for the file dialog box
JFrame guiFrame = new JFrame();
JFileChooser fileDialog = new JFileChooser();
int saveChoice = fileDialog.showSaveDialog(guiFrame);

Like the showOpenDialog method the showSaveDialog method also returns an int value that represents the choice the user has made:

if (saveChoice == JFileChooser.APPROVE_OPTION)
{
    File selectedFile = fileDialog.getSelectedFile();
}

Setting a Directory

Generally it's nice for a user if you set the directory of a file dialog to be close to where the files they are working with are. There are a couple of ways to set the directory. The first option is to set the directory when you make the JFileChooser instance by providing it with a String or File object:

JFileChooser fileDialog = new JFileChooser("C:\\Documents and Settings\\Owner\\My Documents");

The second is to set the directory using the setCurrentDirectory method:

fileDialog.setCurrentDirectory(new File("C:\\Documents and Settings\\Owner\\My Documents");

The setCurrentDirectory needs a file object passed to it in order to set the directory.

Setting File Filters

If your application is only interested in files of a certain type it's easy to set a filter for the file dialog so that it only shows files with the required extensions. This is done by using the setFileFilter method:

fileDialog.setFileFilter(new FileNameExtensionFilter("Text Files", "txt", "rtf"));

The setFileFilter method takes a FileNameExtensionFilter object to set the file filter. In the above example I've limited the file dialog to only accept text files with the extensions .txt and .rtf.

To find out more about the JFileChooser class have a look at JFileChooser Overview. There is also an example Java code program to be looked at in the File Dialog Box Program.

©2014 About.com. All rights reserved.