title: Java: How to cancel application exit
body: <p>On one of my programs, I want a Dialog to appear when the user attempts to exit the application. The user must then choose to save some state of the program, not to save or to cancel the exit operation.</p>
  
  <p>I wrote this in an attempt to find a solution first and ony then implement it:</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.Dimension;
  import java.awt.event.*;
  
  class WL implements WindowListener
  {
      private boolean statussaved;
      private JFrame tframe;
  
      WL (JFrame frame)
      {
          statussaved = false;
          tframe = frame;
      }
  
      @Override public void windowActivated (WindowEvent w) { }
      @Override public void windowClosed (WindowEvent w) { }
      @Override public void windowDeactivated (WindowEvent w) { }
      @Override public void windowDeiconified (WindowEvent w) { }
      @Override public void windowIconified (WindowEvent w) { }
      @Override public void windowOpened (WindowEvent w) { }
  
      @Override public void windowClosing (WindowEvent w)
      {
          if (statussaved)
          {
              return;
          }
  
          final JDialog diag = new JDialog (tframe, "Save Progress", true);
          diag.setPreferredSize (new Dimension (500, 100));
          diag.setResizable (false);
          diag.setDefaultCloseOperation (JDialog.DISPOSE_ON_CLOSE);
  
          JPanel notifypanel = new JPanel ();
          notifypanel.add (new JLabel ("Do you want to save the current status ?"));
  
          JButton yesbutton = new JButton ("Yes");
          JButton nobutton = new JButton ("No");
          JButton cancelbutton = new JButton ("Cancel");
  
          yesbutton.addActionListener (new ActionListener ()
          {
              @Override public void actionPerformed (ActionEvent a)
              {
                  //SAVE THE STATUS
  
                  System.out.println ("Saving status...");
                  statussaved = true;
  
                  diag.dispose ();
                  tframe.dispose ();
              }
          });
  
          nobutton.addActionListener (new ActionListener ()
          {
              @Override public void actionPerformed (ActionEvent a)
              {
                  //just exit/close the application
  
                  diag.dispose ();
                  tframe.dispose ();
              }
          });
  
          cancelbutton.addActionListener (new ActionListener ()
          {
              @Override public void actionPerformed (ActionEvent a)
              {
                  //DON'T EXIT !!!
  
                  diag.dispose ();
              }
          });
  
          notifypanel.add (yesbutton);
          notifypanel.add (nobutton);
          notifypanel.add (cancelbutton);
  
          diag.setContentPane (notifypanel);
  
          diag.pack ();
          diag.setVisible (true);
      }
  }
  
  public class SaveTest
  {
      public static void main (String[] args)
      {
          SwingUtilities.invokeLater (new Runnable ()
          {
              @Override public void run ()
              {
                  JFrame frame = new JFrame ("Save Test");
                  frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
                  frame.addWindowListener (new WL (frame));
  
                  JLabel lab = new JLabel ("just some information");
  
                  frame.setPreferredSize (new Dimension (400, 300));
                  frame.setResizable (false);
                  frame.add (lab);
                  frame.pack ();
                  frame.setVisible (true);
              }
          });
      }
  }
  </code></pre>
  
  <p>It compiles and runs without any change, so you can test it.</p>
  
  <p>The "Yes" and "No" choices work as expected, but I have absolutely no idea what to write in the <code>ActionListener</code> of the "Cancel" button. What I want is, when the user clicks the "Cancel" button, the dialog dissapears, but the main window remains visible (i.e. the program keeps running).</p>
  
  <p>Now, since all this is implemented in the <code>windowClosing</code> method, it's kind of clear that some sort of dispose signal was sent in order to destroy the <code>JFrame</code>. This means that there is probably no way this can be done in the current design. I don't mind reorganizing/redesigning all this to make it work. It's just... I don't know how.</p>
  
  <p>Any ideas ?</p>