Reacting To State Changes
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) {
final Group group = new Group();
Scene scene = new Scene(group, 300, 150);
stage.setScene(scene);
stage.setTitle("Sample");
Task<Integer> task = new Task<Integer>() {
@Override protected Integer call() throws Exception {
int iterations = 0;
for (iterations = 0; iterations < 10; iterations++) {
if (isCancelled()) {
break;
}
System.out.println("Iteration " + iterations);
}
return iterations;
}
@Override protected void succeeded() {
super.succeeded();
updateMessage("Done!");
}
@Override protected void cancelled() {
super.cancelled();
updateMessage("Cancelled!");
}
@Override protected void failed() {
super.failed();
updateMessage("Failed!");
}
};
task.run();
System.out.println(task.getMessage());
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Related examples in the same category