// 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.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class ExitHandling extends JFrame implements WindowListener {
    public ExitHandling() {
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setTitle("Exit handling demo");
        setSize(300, 300);
        addWindowListener(this);
    }
    @Override
    public void windowActivated(WindowEvent e) {
    }
    @Override
    public void windowClosed(WindowEvent e) {
    }
    @Override
    public void windowClosing(WindowEvent e) {
        switch(JOptionPane.showConfirmDialog(null, "Save?")) {
            case JOptionPane.YES_OPTION:
                // save file
                dispose();
                break;
            case JOptionPane.NO_OPTION:
                dispose();
                break;
            case JOptionPane.CANCEL_OPTION:
                // nothing
                break;
        }
    }
    @Override
    public void windowDeactivated(WindowEvent e) {
    }
    @Override
    public void windowDeiconified(WindowEvent e) {
    }
    @Override
    public void windowIconified(WindowEvent e) {
    }
    @Override
    public void windowOpened(WindowEvent e) {
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new ExitHandling();
                f.setVisible(true);
            }
        });
    }
}
