/**
 * @(#)SaveFileDialogExample.java 1.0
 * This code is written by www.codejava.net
 *
 */

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class SaveFileDialogExample extends JFrame {

	private JButton buttonBrowse;

	public SaveFileDialogExample() {
		super("Save File Dialog Example");
		setLayout(new FlowLayout());
		buttonBrowse = new JButton("Save...");
		buttonBrowse.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				showSaveFileDialog();
			}
		});
		getContentPane().add(buttonBrowse);
		setSize(300, 100);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) { }

        SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				new SaveFileDialogExample();
			}
		});
	}

	private void showSaveFileDialog() {
            String fs = System.getProperty("file.separator");
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setDialogTitle("Specify a file to save");
            fileChooser.setSelectedFile(new File("" + System.getProperty("user.dir") 
                + fs + "Incidents" + fs + "inc_123.xml"));

            int userSelection = fileChooser.showSaveDialog(this);
            if (userSelection == JFileChooser.APPROVE_OPTION) {
                    File fileToSave = fileChooser.getSelectedFile();
                    System.out.println("Save as file: " + fileToSave.getAbsolutePath());
            }
	}
}
