I am a beginner in java .So please let me know how can i simultaneously run the progress bar and my application code together.In other words i want to make my progressbar to increment as long as my application code is doing some processing.Please elaborate it with code.
What i think is that i have to add two threads simultaneously.One thread updating the progressbar thread but i am not sure whether its the right way or not.
i have written this code for incrementing progress bar (hard coded)
class ProgressMonitor implements Runnable {
public void run() {
jProgressBar1.setStringPainted(true);
//run untill 100% complete
while (progress < 100) {
//update the progressbar
jProgressBar1.setValue(++progress);
jProgressBar1.repaint();
try {
//Sleep for .25 second
Thread.sleep(250);
} catch (InterruptedException ex) {
}
}
}
The problem is basically that the long running task blocks the Event Dispatch Thread that updates the GUI. One solution is to do the work in a
SwingWorker.See also the Concurrency in Swing lesson of the tutorial for more details.