我有List
个任务,这些任务都会更新名为totalProgress
的常用进度条。此栏分别更新每项任务。我已经尝试创建一个DoubleBinding
对象,以便能够根据每个任务的平均进度更新进度条,但它无法正常工作。
以下是我尝试使用的代码:
double p = 0.0;
for (final Task t : tasks) {
threadPool.submit(t);
DoubleBinding totalProgress = Bindings.createDoubleBinding(new Callable<Double>() {
@Override
public Double call() {
p = (p+t.getProgress()) / tasks.size();
System.out.println("P"+p + "Progress"+t.getProgress());
return p;
}
});
bar.progressProperty().bind(totalProgress);
//bar.progressProperty().bind(t.progressProperty());
t.setOnSucceeded(handler);
}
threadPool.shutdown();
答案 0 :(得分:1)
与您链接的示例中的方式相同:
Observable[] progressProps = new Observable[tasks.size()];
for (int i = 0 ; i < tasks.size(); i++) {
progressProps[i]=tasks.get(i).progressProperty();
}
DoubleBinding totalProgress = Bindings.createDoubleBinding(new Callable<Double>() {
@Override
public Double call() {
double total = 0 ;
for (Task task : tasks) {
total = total + task.getProgress();
}
return total / tasks.size();
}
}, progressProps);
bar.progressProperty().bind(totalProgress);
for (final Task t : tasks) {
threadPool.submit(t);
t.setOnSucceeded(handler);
}