title: Make a swing thread that show a "Please Wait" JDialog
body: <p>The problem is this:<br/>
  I've a swing application running, at a certain point a dialog requires to insert username and password and to press "ok". <br/>
  I would like that when the user press "ok" the swing application does in this order:</p>
  
  <ol>
  <li>Open a "Please wait" JDialog <br/></li>
  <li>Make some operation(eventually displaying some other JDialog or JOptionPane)<br/></li>
  <li>When it finishes with the operation close the "please wait" JDialog<br/></li>
  </ol>
  
  <p>This is the code that I wrote in the okButtonActionPerformed():</p>
  
  <pre><code>private void okButtonActionPerformed(java.awt.event.ActionEvent evt) { 
      //This class simply extends a JDialog and contains an image and a jlabel (Please wait)
      final WaitDialog waitDialog = new WaitDialog(new javax.swing.JFrame(), false);    
      waitDialog.setVisible(true);
      ... //Do some operation (eventually show other JDialogs or JOptionPanes)
      waitDialog.dispose()
  }
  </code></pre>
  
  <p>This code obviously doesn't works because when I call the waitDialog in the same thread it blocks all till I don't close it. <br/>
  So I tried to run it in a different thread:</p>
  
  <pre><code>private void okButtonActionPerformed(java.awt.event.ActionEvent evt) { 
      //This class simply extends a JDialog and contains an image and a jlabel (Please wait)
      final WaitDialog waitDialog = new WaitDialog(new javax.swing.JFrame(), false);    
      SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
              waitDialog.setVisible(true);
          }
      });
      ... //Do some operation (eventually show other JDialogs or JOptionPanes)
      waitDialog.dispose()
  }
  </code></pre>
  
  <p>But also this doesn't work because the waitDialog is not displayed immediately but only after that the operation completed their work (when they show a joption pane "You are logged in as...")<br/><br/></p>
  
  <p>I also tried to use invokeAndWait instead of invokeLater but in this case it throws an exception: </p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread
  </code></pre>
  
  <p>How can I do?</p>