extends Task : Task « JavaFX « Java






extends 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 {

  @Override
  public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 150);
    stage.setScene(scene);
    stage.setTitle("Sample");

    final int totalIterations = 90;

    IteratingTask task = new IteratingTask(totalIterations);
 
    task.run();
    System.out.println(task.getMessage());

    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}
class IteratingTask extends Task<Integer> {
  private final int totalIterations;

  public IteratingTask(int totalIterations) {
      this.totalIterations = totalIterations;
  }

  @Override protected Integer call() throws Exception {
      int iterations = 0;
      for (iterations = 0; iterations < totalIterations; iterations++) {
          if (isCancelled()) {
              updateMessage("Cancelled");
              break;
          }
          updateMessage("Iteration " + iterations);
          updateProgress(iterations, totalIterations);
      }
      return iterations;
  }
}

   
  








Related examples in the same category

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