views:

265

answers:

1

I want to prompt the user for a confirmation when they want to close the frame window, like so:

addWindowListener(new WindowAdapter()
{
    public void windowClosing(WindowEvent e)
    {
        if (JOptionPane.showConfirmDialog(null, "Are you sure you want to abort the game?",
      "Really quit?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
        dispose();
    }
});

The dialog shows up but the window closes no matter if I click yes, no, or close the window. Is this some sort of bug or am I really missing something simple here? I'm using Eclipse on OS X with Java 1.6.0.13.

+3  A: 

You have to set the frame to do nothing on close to control the closing:

 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
Yishai