3
public void myMethod {
   MyProgessBarFrame progFrame = new MyProgressBarFrame(); // this is a JFrame
   progFrame.setVisible(true); // show my JFrame loading

   // do some processing here while the progress bar is running
   // .....

   progFrame.setvisible(false); // hide my progress bar JFrame
} // end method myMethod

I have the code above. But when I run it, the do some processing section does not process until I close the progress bar JFrame.

How will I show my progress bar and tell Java to continue in the do processing section?

gprathour
  • 14,813
  • 5
  • 66
  • 90
epsac
  • 198
  • 5
  • 17

2 Answers2

9

You've got a classic problem with concurrency and Swing. Your problem is that you're doing a long-running task on the main Swing thread, the EDT or Event Dispatch Thread, and this will lock the thread until the process is complete, preventing it from doing its tasks including interacting with the user and drawing GUI graphics.

The solution is to do the long-running task in a background thread such as that given by a SwingWorker object. Then you can update the progressbar (if determinant) via the SwingWorker's publish/process pair. For more on this, please read this article on Concurrency in Swing.

e.g.,

public void myMethod() {
  final MyProgessBarFrame progFrame = new MyProgessBarFrame();
  new SwingWorker<Void, Void>() {
     protected Void doInBackground() throws Exception {

        // do some processing here while the progress bar is running
        // .....
        return null;
     };

     // this is called when the SwingWorker's doInBackground finishes
     protected void done() {
        progFrame.setVisible(false); // hide my progress bar JFrame
     };
  }.execute();
  progFrame.setVisible(true);
}

Also, if this is being displayed from another Swing component, then you should probably show a modal JDialog not a JFrame. This is why I called setVisible(true) on the window after the SwingWorker code -- so that if it is a modal dialog, it won't prevent the SwingWorker from being executed.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Wow! seems like I need to understand a lot of concepts before successfully do GUI programming in Java. I tried your code and it really works, although I can't still understand the theory behind it. I will read the link that you have provided as soon as I delivered my project. Thanks a lot! – epsac Nov 24 '11 at 03:14
  • @Jec: That's a truism not for just GUI programming, but for programming in general. It's like finding a never-ending Matryoshka doll. – Hovercraft Full Of Eels Nov 24 '11 at 03:18
2

You would have noticed that when a progress bar is shown, along with processing of the progress bar the actual task is also being done. So you can understand from here that there is multi-threading going on.

In Java you can make a separate thread to just show the progress bar and in main thread you can do your task. So both the processes will run simultaneously and it will serve your need.

*NOTE : the progress shown in progress bar should depend on processing being done in main thread.

you can check these links for Threads & Progress Bar in Java.

gprathour
  • 14,813
  • 5
  • 66
  • 90