Writing a Task : Task « JavaFX « Java

Writing a Task


import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

  public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 150);

    Task<Integer> task = new Task<Integer>() {
      @Override protected Integer call() throws Exception {
          int iterations;
          for (iterations = 0; iterations < 100000; iterations++) {
              if (isCancelled()) {
              System.out.println("Iteration " + iterations);
          return iterations;


  //  root.getChildren().add(t);


  public static void main(String[] args) {



Related examples in the same category

1.extends Task
2.A Task Which Returns No Value
3.A Task Which Returns An ObservableList
4.A Task Which Modifies The Scene Graph
5.A Task Which Takes Parameters
6.A Simple Loop With Progress Notification
7.A Simple Loop With Progress Notification And Blocking Calls
8.Reacting To State Changes