I’m trying to get a JProgressBar to increment by 1 every 100th of a second, and at the moment I’m using Thread.sleep(100) inside a while statement like so:
try {
while (i<=100){
doTime();
}
} catch (InterruptedException ex) {
Logger.getLogger(SplashScreen.class.getName()).log(Level.SEVERE, null, ex);
}
public void doTime() throws InterruptedException{
jLabel1.setText("sleeping");
Thread.sleep(100);
jLabel1.setText("start");
i++;
pb.setValue(i);
pb.repaint();
}
and If I debug by doing a System.out.println it displays in real time but the swing GUI freezes until it drops out of the while loop can anyone help?
thanks
matt
As derivation said, you should use SwingWorker. From its javadoc:
“SwingWorker is designed for situations where you need to have a long running task run in a background thread and provide updates to the UI either when done, or while processing. Subclasses of SwingWorker must implement the doInBackground() method to perform the background computation.”
With this slight change to ProgressBarDemo.java in the java tutorial, you almost get what you want.