Update JProgressBar from new Thread
Posted
by Dacto
on Stack Overflow
See other posts from Stack Overflow
or by Dacto
Published on 2010-04-28T00:31:03Z
Indexed on
2010/04/28
0:33 UTC
Read the original article
Hit count: 295
How can I update the JProgressBar.setValue(int) from another thread? My secondary goal is do it in the least amount of classes possible.
Here is the code I have right now:
**Part of the main class....**
pp.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
new Thread(new Task(sd.getValue())).start();
}
});
public class Task implements Runnable{
int val;
public Task(int value){
this.val = value;
}
@Override
public void run() {
for (int i=0; i<=value; i++){ //Progressively increment variable i
pbar.setValue(i); //Set value
pbar.repaint(); //Refresh graphics
try{Thread.sleep(50);} //Sleep 50 milliseconds
catch (InterruptedException err){}
}
}
}
pp is a JButton and starts the new thread when the JButton is clicked.
pbar is the JProgressBar object from the Main class.
How can I update its value?(progress)
The code above in run() cannot see the pbar.
© Stack Overflow or respective owner