0

Possible Duplicate:
Java GUI JProgressBar not painting

I have a GUI that has the GUI Locked while processing an Action Event, so I need a progress bar to show up. I can get the JDialog to show up but the progress bar won't show up. I used SwingUtilities.invokeLater() and invokeAndWait() but to no avail. The progress bar will not show up. Any hints or help would be appreciated.

pb = new JProgressBar(0, 100);
pb.setPreferredSize(new Dimension(175, 40));
pb.setString("Working");
pb.setStringPainted(true);
JLabel label = new JLabel("Progress: ");
JPanel center_panel = new JPanel();
center_panel.add(label);
center_panel.add(pb);
dialog = new JDialog((JFrame) null, "Working ...");
dialog.getContentPane().add(center_panel, BorderLayout.CENTER);
dialog.pack();
dialog.setLocationRelativeTo(this); // center on screen
dialog.toFront(); // raise above other java windows
SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
        dialog.setVisible(true);
        pb.setIndeterminate(true);
    }
});
Thread.sleep(5000);
template = AcronymWizardController
    .sharedInstance().readAndDislpayDocx(contdFile);
parseDocxText(contdFile);
pb.setIndeterminate(false);
savedFile.setText(contdFile.toString());
dialog.dispose();
Community
  • 1
  • 1
yams
  • 942
  • 6
  • 27
  • 60
  • 1
    @JBNizet It's a duplicate to about half the Swing questions that get asked here ;) – MadProgrammer Dec 22 '12 at 22:56
  • 2
    @MadProgrammer: yes indeed. And that makes it even worse to ask it once again, hence my close vote. I wonder how it's possible not to find the answer. The duplicate question I linked to is the first question in the "Related" section on the right. – JB Nizet Dec 22 '12 at 22:58
  • @JBNizet Hay, you got +1 from me ;) – MadProgrammer Dec 22 '12 at 23:47

1 Answers1

4

Swing is a single threaded API, that is, all the UI updates and modifications are performed from within a single thread (known as the Event Dispatching Thread or EDT). Anything that blocks this thread will stop it from processing additional updates, like repaints.

You have a number of choices. Your immediate requirement is to move the long running task off the EDT. To do this you can either use a SwingWorker or a Thread.

From your description, a SwingWorker will be easier.

For a simple example, check out JProgressBar won't update

For more information, you should check out Concurrency in Swing

You other choice would be to use something like a ProgressMonitor, example here

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366