Displaying a dialog window to save files

This following example will use Swings JFileChooser to display a save dialog box and allow the user to make a selection.
It will set the current directory to c: and set the default selection to autoexec.bat. Notice that multiple file selections are not implemented for the current look and feels (see the JFileChooser API).
The method showSaveDialog will block until a file has been selected. It returns APPROVE_OPTION if OK has been clicked, CANCEL_OPTION otherwise.

import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Main extends JFrame
   public static void main(String []args) {
      Main main = new Main();
   public Main() {
      JButton fileButton = new JButton("Select File");
      fileButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            File file = getFileFromUser();
            if (file != null)
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
   public File getFileFromUser() {
      JFileChooser fc = new JFileChooser();
      // use current directory
      fc.setCurrentDirectory(new File("c:\"));
      // set default name
      fc.setSelectedFile(new File("autoexec.bat"));
      // show dialog for opening files
      int result = fc.showSaveDialog(this);
      if (result != fc.APPROVE_OPTION) 
         return null;
      return fc.getSelectedFile();