title: Add dialog box when the program is clossed
body: <p>I want to add an option when somebody closes the frame to check if are there any thread running and if there are any I want to display a dialog box which should ask the user if he really wants to exit. The problem is that no matter which is the option of the user the program closes anyway.</p>
  
  <p>Here is what I tried:</p>
  
  <pre><code>private void addActionWhenFrameIsClosed( )
  {
  
      addWindowListener( new WindowAdapter( )
      {
          public void windowClosing( WindowEvent e )
          {
              if( isThereAnyThreadRunning( ) )
              {
                  int dialogButton = JOptionPane.YES_NO_OPTION;
                  int dialogResult = JOptionPane.showConfirmDialog( null, "There are some threads running... "
                                                                          + "\nAre you sure you want to exit?",
                                                                    "Warning", dialogButton );
  
                  if( dialogResult == JOptionPane.YES_OPTION)
                  {
                      dispose( );
                  }
  
              }
          }
      } );
  }
  </code></pre>