1. Technology

JFileChooser Overview

By

Man sitting on couch working with a laptop
J Shepherd/Photographer's Choice RF/Getty Images

The JFileChooser Class

Overview:

The JFileChooser class provides the functionality of a file dialog box. It allows users to navigate through their file system to choose a directory to either open or save a file.

Import statement:

import javax.swing.JFileChooser;

Constructors:

To use a JFileChooser you need to create an instance by using one of 6 constructors depending on how you want the file dialog configured on creation:

	• To open at the user's default directory use:

	    JFileChooser fileDialog = new JFileChooser();
	
	•To open at a specified directory you can pass the directory as a File object: 

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

         or as a String:

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

	•To open the file directory using a specific file system view. These options are used if you want to 
         customize how the icons of the files in the file dialog are shown. To do this you need to create 
         a class that extends the FileView class.

JFileChooser Show Methods

There are three different dialogs that can be presented to the user:

  • Open File Dialog - use the showOpenDialog method:
  • Save File Dialog - use the showSaveDialog method.
  • Custom Text File Dialog - use the showDialog method. Pass the text you want the button to have in a String: fileDialog.showDialog(guiFrame, "Bring Me The Files!");

Filtering File Types

To make the file dialog only accept certain file types you need to use a FileNameExtensionFilter object and the setFileFilter method. For example, to only accept text files with the extensions .txt and .rtf:

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

Getting and Setting File Name

If you want to find out which file the user selected use the getSelectedFile method:

File openFile = fileDialog.getSelectedFile();

The getSelectedFile method returns a File object which can be used to get the file's name, path and properties. Likewise if you want to give the user a default filename you can pass a File object to the setSelectedFile method:

The Current Directory

If you want to set a specific directory for the file dialog after an instance of the JFileChooser class is created then use the setCurrentDirectory method with a File object:

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

To set it to the current working directory:

fileDialog.setSelectedFile(new File("."));

To find out what the current directory of the JFileChooser object is set to use the getCurrentDirectory method:

File currentDirectory = fileDialog.getCurrentDirectory();

Usage Tips:

  • A file dialog box created using JFileChooser is always modal.
  • The int value returned from the showOpenDialog, showSaveDialog and showDialog methods shows the choice the user made: cancel button - JFileChooser.CANCEL_OPTION, open/save button - JFileChooser.APPROVE_OPTION, and dialog dismissed - JFileChooser.ERROR_OPTION.
  • A parent component is passed in the showOpenDialog, showSaveDialog and showDialog methods to determine the location on the screen the file dialog will appear. This means that you can use the same JFileChooser object many times with different frames as the parent.

Have a look at Presenting a File Dialog to the User for more on using the showOpenDialog and showSaveDialog methods. There is also an example Java code program in the File Dialog Program.

  1. About.com
  2. Technology
  3. Java
  4. Java Syntax
  5. Utilizing the Java API
  6. javax.swing
  7. Java Syntax - JFileChooser Overview

©2014 About.com. All rights reserved.