-1

I have a List of tasks that all update a common progress bar called totalProgress. This bar updates for each task seperately. I have tried creating a DoubleBinding object to be able to update the progress bar based on the average progress of each task, but it is not working.

Referred post: Multiple Tasks JavaFX

Here is the code I am trying to work with:

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();
Anil
  • 655
  • 1
  • 11
  • 25
  • Why don't you do it the same way as the post you linked? – James_D Nov 07 '16 at 18:20
  • I tried it but not able to use task1.progressProperty(), task2.progressProperty()) in the Double Binding Method the ide gives an error and also i have a list of tasks and those are not fixed. Like i add tasks to a list of tasks and execute from the list –  Nov 07 '16 at 18:25
  • I did this @James_D , this was the best i could i know its lame but i tried a lot on this , sorry for the earlier comment by mistake it got posted –  Nov 07 '16 at 18:27
  • When i print the progress it commes fine but i guess there is some mistake in binding ?Which i am unable to rectify –  Nov 07 '16 at 18:28

1 Answers1

1

Do it the same way as in the example you linked:

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);
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thanks @James_D for the answer but can you please explain why this line tasks.toArray(new Task[0]). and why is the compiler telling to cast the line to Observable[] –  Nov 07 '16 at 18:34
  • And also its now throwing Exception of Class Caste , Can you please me that line , i am new to bindings –  Nov 07 '16 at 18:37
  • Oops, yeah, that won't work... You need an array of observables; the binding will be bound to those, so if any of those change, the binding knows its value has changed. So that argument needs to be an array containing the progress properties (not an array containing the tasks). See updated code. – James_D Nov 07 '16 at 18:38
  • Just one thing more we need to add the cast of DoubleProperty on task.getProperty as it is a ReadonlyDoubleProperty –  Nov 07 '16 at 18:47