I try to understand how to update a ProgressBar in a multithreaded environment. Iam doing something wrong here but I dont see what it is. This should simply fill up the bar every 3sec but it doesnt:
Task<Void> task = new Task<Void>(){
@Override
public Void call(){
for (int i = 1; i < 10; i++) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
updateProgress(i , 10);
}
return null;
}
};
updProg = new ProgressBar();
updProg.progressProperty().bind(task.progressProperty());
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
What am I missing?
Your sample works fine for me.
The sample fills the bar a little bit every 3 seconds, completely filling the bar in half a minute.
I just wrapped it in some scaffolding code to make it executable and it worked without change (java7u15, win7).
Perhaps you have been using some early access version of Java 8 which had a bug in it (now fixed) around ProgressBar updates.
RT-29018 ProgressBar and ProgressIndicator disappear when progressProperty is updated
If you defined updProg in the FXML file, then the problem could be the initialization here.
try to just remove this line:
Are you using the latest JDK 8 Early-Access? If so, see this bug report I filed: http://javafx-jira.kenai.com/browse/RT-29018
Basically, in a recent release of the early-access build, they made some changes in the skins and css. This caused a hidden bug to be revealed in which child nodes which are dirtier than parent nodes but both need repainting in the same pulse, the parent's dirty level ends up overwriting the child nodes dirty level.
This causes the progress to not show, and in fact, for me the
progressBar
became entirely invisible, as soon asupdateProgress
was called from the task. They have a patch in place, I don't know when this will go through.A work-around, either use jdk7 while waiting on the patch, or you can do what I did and apply this from the old css into your css stylesheet: