I want to use ControlsFX StatusBar to display the progress of a long running task. Problem is: In the following code the progress bar displays only once, although the console output is fine every time the task runs. Am I missing something?
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
TilePane root = new TilePane();
Scene scene = new Scene(root, 500, 200);
Button btn = new Button("Start");
StatusBar status = new StatusBar();
status.setPrefWidth(200);
root.getChildren().addAll(btn, status);
primaryStage.setScene(scene);
primaryStage.show();
StatusTestService service = new StatusTestService();
status.progressProperty().bind(service.progressProperty());
status.progressProperty().addListener(inv -> System.out.println(status.getProgress()));
btn.setOnAction(event -> service.restart());
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(System.getProperty("java.version"));
launch(args);
}
class StatusTestService extends Service <Void> {
@Override
protected Task <Void> createTask() {
return new Task <Void> () {
@Override
protected Void call() throws Exception {
for (int i = 0; i < 10; i++) {
Thread.sleep(100);
updateProgress(i, 9);
}
return null;
}
};
}
}