The JFileChooser Class

Since it plays such an integral role in just about every commercial application, let’s look at the file chooser first. The JFileChooser class bundles a directory pane and typical selection buttons into a handy interface.
 
The application itself reports only which file (or files, if you use the Open option) you chose to open or save. Our application has a Pick Directory button that restricts the chooser to directories. The event handlers for each button do most of the interesting work. In each case, we create a new JFileChooser object, make any changes to the default properties that we need for the particular action, and then show the dialog. As you will see from the constants discussed later, the int returned from the showDialog( ) method indicates whether the user accepted a file selection or canceled the dialog. If we have a successful selection, our example application puts the name of the file into a display label.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class SimpleFileChooser extends JFrame {

	public SimpleFileChooser() {
		super("File Chooser Test Frame");
		setSize(350, 200);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		Container c = getContentPane();
		c.setLayout(new FlowLayout());

		JButton openButton = new JButton("Open");
		JButton saveButton = new JButton("Save");
		JButton dirButton = new JButton("Pick Dir");
		final JLabel statusbar = new JLabel("Output of your selection will go here");

		// Create a file chooser that opens up as an Open dialog.
		openButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				JFileChooser chooser = new JFileChooser();
				chooser.setMultiSelectionEnabled(true);
				int option = chooser.showOpenDialog(SimpleFileChooser.this);
				if (option == JFileChooser.APPROVE_OPTION) {
					File[] sf = chooser.getSelectedFiles();
					String filelist = "nothing";
					if (sf.length > 0)
						filelist = sf[0].getName();
					for (int i = 1; i < sf.length; i++) {
				filelist += ", " + sf[i].getName();
					}
					statusbar.setText("You chose " + filelist);
				} else {
					statusbar.setText("You canceled.");
				}
			}
		});

		// Create a file chooser that opens up as a Save dialog.
		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				JFileChooser chooser = new JFileChooser();
				int option = chooser.showSaveDialog(SimpleFileChooser.this);
				if (option == JFileChooser.APPROVE_OPTION) {
					statusbar.setText("You saved "
							+ ((chooser.getSelectedFile() != null) ? 
                              chooser.getSelectedFile().getName() : "nothing"));
				} else {
					statusbar.setText("You canceled.");
				}
			}
		});

		// Create a file chooser that allows you to pick a directory
		// rather than a file.
		dirButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int option = chooser.showOpenDialog(SimpleFileChooser.this);
				if (option == JFileChooser.APPROVE_OPTION) {
					statusbar.setText("You opened "
							+ ((chooser.getSelectedFile() != null) ? 
                              chooser.getSelectedFile().getName() : "nothing"));
				} else {
					statusbar.setText("You canceled.");
				}
			}
		});

		c.add(openButton);
		c.add(saveButton);
		c.add(dirButton);
		c.add(statusbar);
	}

	public static void main(String args[]) {
		SimpleFileChooser sfc = new SimpleFileChooser();
		sfc.setVisible(true);
	}
}