title: Executing code after SwingWorker.execute()...AFTER it
body: <p>In my application, I would like to do something like </p>
  
  <ul>
  <li>show a wait dialog, where some preparatory task is happening in the background</li>
  <li>after that preparatory task is done, close the dialog</li>
  <li>when dialog is closed (or <code>SwingWorker.done()</code>, execute the rest of my code)</li>
  </ul>
  
  <p>I started noticing problems: the code <strong>after</strong> my <code>new WaitScreen().execute();</code> was being executed <strong>before</strong> it and I had to manually close the dialog to trigger the <code>done()</code>. </p>
  
  <p>To try to make sense of it, I took some <a href="https://stackoverflow.com/questions/13131061/safely-open-and-close-modal-jdialog-using-swingworker/13131561#13131561">simpler code</a>, made it my own, and used it to emulate my problem. Here it is: </p>
  
  <p>import java.awt.EventQueue;
  import java.awt.BorderLayout;</p>
  
  <pre><code>import javax.swing.JDialog;
  import javax.swing.JLabel;
  import javax.swing.SwingUtilities;
  import javax.swing.SwingWorker;
  import javax.swing.UIManager;
  
  public class SwingWorkerDialogTest 
  {
  
      public static void main(String[] args) 
      {
          new SwingWorkerDialogTest();
      }
      private JDialog dialog;
  
      public SwingWorkerDialogTest() 
      {
          EventQueue.invokeLater(new Runnable() 
          {
              @Override
              public void run() 
              {
                  try 
                  {
                      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                  }
                  catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) 
                  {
  
                  }
                  catch (javax.swing.UnsupportedLookAndFeelException ex) 
                  {
  
                  }
  
                  MyWorker worker = new MyWorker(true);
                  worker.execute();
                  new MyWorker(false).execute();
              }
          });
      }
  
      public class MyWorker extends SwingWorker&lt;Object, Object&gt; 
      {
          private boolean runGUI;
          public MyWorker(boolean b) { this.runGUI = b; }
          @Override
          protected Object doInBackground() throws Exception 
          {
              SwingUtilities.invokeLater(new Runnable() 
              {
                  @Override
                  public void run() 
                  {
                      if (runGUI)
                      {
                          getDialog().setVisible(true);
                      }
                      else
                      {
                          for (int j = 0; j &lt; 30; j++)
                          {
                              System.out.println("j == " + j);
                          }
                      }
                  }
              });
              Thread.sleep(20000);
              return null;
          }
  
          @Override
          protected void done() 
          {
              System.out.println("now in done...");
              JDialog dialog = getDialog();
              // Don't care, dismiss the dialog
              //dialog.setVisible(false);
              dialog.dispose();
          }
  
      }
  
      protected JDialog getDialog() 
      {
          if (dialog == null) 
          {
              dialog = new JDialog();
              dialog.setModal(true);
              dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
              dialog.setLayout(new BorderLayout());
              dialog.add(new JLabel("Please wait..."));
              dialog.setSize(200, 200);
              dialog.setLocationRelativeTo(null);
          }
          return dialog;
      }
  
  }
  </code></pre>
  
  <p>It was working fine when there was only <code>worker</code> running. But, the instant I <code>execute()</code>d another <code>MyWorker</code>, my problem reoccurred. How do I remedy this?</p>