Reputation: 1
I've a simple gui created with javaFx (java version 1.8.101) with a button and progress bar. I want to update the progress immediately after pressed the button. Inside the "event press button" I also have a simple for loop that prints a message for a number of times . The problem is that the progress bar is updated only at the end of print message loop and not at the same time independently from it. Here is the controller class
package sample;
import com.sun.javafx.tk.Toolkit;
import javafx.beans.property.Property;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ProgressBar;
public class Controller {
@FXML
ProgressBar progressBar;
@FXML
private void handleStartBtnAction(ActionEvent event) throws InterruptedException {
final int size = 100;
Task<Integer> t = new Task<Integer>() {
@Override
protected Integer call() throws Exception {
int iterations;
for (iterations = 0; iterations < size; iterations++) {
if (isCancelled()) {
break;
}
updateProgress(iterations, size);
System.out.println("update message " + iterations);
Thread.sleep(100);
}
return iterations;
}
};
this.progressBar.progressProperty().bind((Property<Number>) t.progressProperty());
Thread th = new Thread(t);
th.setDaemon(true);
th.start();
for (int i = 0; i < 500000; i++) {
System.out.println("print message " + i);
}
}
}
Upvotes: 0
Views: 731