// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class AskFirst extends JFrame implements ActionListener {
    private JButton save;
    public AskFirst() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());
        save = new JButton("Save");
        save.addActionListener(this);
        getContentPane().add(save, BorderLayout.CENTER);
        pack();;
    }
    public void actionPerformed(ActionEvent ev) {
        JFileChooser chooser = new JFileChooser(System.getProperty("user.home"));
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
             File f = chooser.getSelectedFile();
             if(f.exists()) {
                 if(JOptionPane.showOptionDialog(this, "Want to overwrite?", "Confirm dialog", JOptionPane.YES_NO_OPTION,
                                                 JOptionPane.QUESTION_MESSAGE, null, new String[] { "Yes", "No"}, "No") == 0) {
                     JOptionPane.showMessageDialog(this, "Existing file " + f.getAbsoluteFile() + " saved");
                 } else {
                     JOptionPane.showMessageDialog(this, "Existing file " + f.getAbsoluteFile() + " *NOT* saved");
                 }
             } else {
                 JOptionPane.showMessageDialog(this, "New file " + f.getAbsoluteFile() + " saved");
             }
        }
    }
    public static void main(String[] args) {
        AskFirst f = new AskFirst();
        f.setVisible(true);
    }
}
