title: Java - global, reusable loading dialog
body: <p>I'm trying to implement a global loading dialog... I want to call some static function to show the dialog and some static function to close it. In the meanwhile I'm doing some work in the main thread or in a sub thread...</p>
  
  <p>I tried following, but the dialog is not updating... Just once at the end, before hiding it again, it updates...</p>
  
  <pre><code>    private static Runnable getLoadingRunable()
  {
      if (loadingRunnable != null)
      {
          loadingFrame.setLocationRelativeTo(null);
          loadingFrame.setVisible(true);  
          return loadingRunnable;
      }
  
      loadingFrame = new JFrame("Updating...");
      final JProgressBar progressBar = new JProgressBar();
      progressBar.setIndeterminate(true);
      final JPanel contentPane = new JPanel();
      contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
      contentPane.setLayout(new BorderLayout());
      contentPane.add(new JLabel("Updating..."), BorderLayout.NORTH);
      contentPane.add(progressBar, BorderLayout.CENTER);
      loadingFrame.setContentPane(contentPane);
      loadingFrame.pack();
      loadingFrame.setLocationRelativeTo(null);
      loadingFrame.setVisible(true);  
  
      loadingRunnable = new Runnable() {
          public void run() {
  
              try {
                  while (running) {
                      Thread.sleep(100);
                  }
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
  
              SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      loadingFrame.setVisible(false);
                  }
              });
          }
      };
      return loadingRunnable;
  }
  
  public static void showLoadingBar() {
      System.out.println("showLoadingBar");
      running = true;
  
      threadLoadingBar = new Thread(getLoadingRunable());
      threadLoadingBar.start();
  }
  
  public static void hideLoadingBar() {
      System.out.println("hideLoadingBar");
      running = false;
      threadLoadingBar = null;
  }
  </code></pre>