Return to Snippet

Revision: 20530
at November 18, 2009 05:43 by liqweed


Initial Code
/** This action creates and shows a modal save-file dialog. */
public abstract static class FileAction extends AbstractAction {

	private final JFileChooser chooser = new JFileChooser();
	private final JPanel parent;

	FileAction(JPanel parent) {
		this.parent = parent;
	}

	@Override public void actionPerformed(ActionEvent evt) {
		// Show dialog; won't return until dialog is closed:
		chooser.showSaveDialog(parent);

		// Get the selected file
		File file = chooser.getSelectedFile();
		if (file != null) {
			handleFile(file);
		}
	}

	public abstract void handleFile(File file);
}

...

myButton.addActionListener(new FileAction(getMainPanel()) {
	@Override public void handleFile(File file) {
		// do whatever you want with the file...
	}
});

Initial URL

                                

Initial Description
A Swing ActionListener that opens a JFileChooser and returns the selected file.

Initial Title
A Swing ActionListener that opens a JFileChooser and returns the selected file

Initial Tags
java

Initial Language
Java