Search code examples
javamultithreadingswinguser-interfacejslider

Change JProgressBar value, while Thread is running


I want to change the value of a JSlider in a panel, while I'm running a Thread which give's me this value (because of some progress)

So i calculate the progress-value in the Thread, and with a GET-method I fetch the value in the panel and want to set it to the progressbar.

After debugging it, a value is there but the EDIT:[ JProgressBar ] don't change his UI.

Some code:

while(_thread.isAlive()) {
 pb_calc.setValue(_thread.getVal());
 pb_calc.updateUI();
}

Solution

    • dont use updateUI, this method is reserved for Look and Feel and UIManager

    • output from Background Tasks should be wrapped into invokeLater, more in the tutorial Concurency in Swing

    • better would be invoke Background taks from SwingWorker or inside Runnable#Thread