views:

57

answers:

2

Is there a way to pause/resume the SwingWorker in Java ?

+1  A: 

yes ,

  1. Create a class extending SwingWorker.
  2. Keep a static flag to make pause/resume mechanism in run method.

something like ..

while(){

sleep(1000);
if(shouldRun){

}  

}
org.life.java
+1  A: 

You can pause the task you run in the swing worker by implementing something like this

while(true) {
   if(paused)
   {
       try {
           Thread.sleep(500); // half a second
           continue;
       } catch(InterruptedException e)
       {
       }
   }
}

It is not very nice though.

Fedearne
you copied code from here http://www.velocityreviews.com/forums/t136961-pausing-a-swing-worker-thread.html but forgot to write loop :-)
org.life.java
Correct. Formatted it nicer though.
Fedearne