RSS

How to Use File Choosers in Java

28 Feb

The JFileChooser API makes it easy to bring up open and save dialogs. The type of look and feel determines what these standard dialogs look like and how they differ. In the Java look and feel, the save dialog looks the same as the open dialog, except for the title on the dialog’s window and the text on the button that approves the operation. Here is a picture of a standard open dialog in the Java look and feel:

A standard open dialog shown in the Java look and feel

Java File Chooser Dialogue

Here is a picture of an application called FileChooserDemo that brings up an open dialog and a save dialog.

A program that brings up an open or save dialog
Bringing up a standard open dialog requires only two lines of code:

//Create a file chooser
final JFileChooser fc = new JFileChooser();

//In response to a button click:
int returnVal = fc.showOpenDialog(aComponent);

The argument to the showOpenDialog method specifies the parent component for the dialog. The parent component affects the position of the dialog and the frame that the dialog depends on. For example, the Java look and feel places the dialog directly over the parent component. If the parent component is in a frame, then the dialog is dependent on that frame. This dialog disappears when the frame is minimized and reappears when the frame is maximized.

By default, a file chooser that has not been shown before displays all files in the user’s home directory. You can specify the file chooser’s initial directory by using one of JFileChooser’s other constructors, or you can set the directory with the setCurrentDirectory method.

The call to showOpenDialog appears in the actionPerformed method of the Open a File button’s action listener:

public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);

if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append(“Opening: ” + file.getName() + “.” + newline);
} else {
log.append(“Open command cancelled by user.” + newline);
}
} …
}

The showXxxDialog methods return an integer that indicates whether the user selected a file. Depending on how you use a file chooser, it is often sufficient to check whether the return value is APPROVE_OPTION and then not to change any other value. To get the chosen file (or directory, if you set up the file chooser to allow directory selections), call the getSelectedFile method on the file chooser. This method returns an instance of File.

The example obtains the name of the file and uses it in the log message. You can call other methods on the File object, such as getPath, isDirectory, or exists to obtain information about the file. You can also call other methods such as delete and rename to change the file in some way. Of course, you might also want to open or save the file by using one of the reader or writer classes provided by the Java platform. See Basic I/O for information about using readers and writers to read and write data to the file system.

The example program uses the same instance of the JFileChooser class to display a standard save dialog. This time the program calls showSaveDialog:

int returnVal = fc.showSaveDialog(FileChooserDemo.this);

By using the same file chooser instance to display its open and save dialogs, the program reaps the following benefits:

The chooser remembers the current directory between uses, so the open and save versions automatically share the same current directory.
You have to customize only one file chooser, and the customizations apply to both the open and save versions.

Finally, the example program has commented-out lines of code that let you change the file selection mode. For example, the following line of code makes the file chooser able to select only directories, and not files:

fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

Another possible selection mode is FILES_AND_DIRECTORIES. The default is FILES_ONLY. Note that, in the Java look and feel at least, only directories are visible — not files.

Source: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

 
Comments Off on How to Use File Choosers in Java

Posted by on February 28, 2014 in Java

 

Comments are closed.