title: JavaFX task not reruning once canceled or finishing once
body: <p>I am working on a basic Java FX task exercise. It counts from 1 to 150 on a thread. The current value is presented on a label and updates a progress bar.</p>
  
  <p>There is a button to start the task, to cancel it and to view canceled status of the task.</p>
  
  <p>The thing that puzzles me is as to why I cannot re run the task after having canceled the thread once(same thing happens if I let the task finnish).</p>
  
  <p>I want to be able to rerun the task . Then I need to make it so that it will resume(though that shouldn't be that hard after  figuring out how to rerun the task)</p>
  
  <p>Source ;</p>
  
  <pre><code>public class JavaFX_Task extends Application {
  
  @Override
  public void start(Stage primaryStage) {
  
      final Task task;
      task = new Task&lt;Void&gt;() {
          @Override
          protected Void call() throws Exception {
              int max = 150;
              for (int i = 1; i &lt;= max; i++) {
                  if (isCancelled()) {
                      break;
                  }
                  updateProgress(i, max);
                  updateMessage(String.valueOf(i));
  
                  Thread.sleep(100);
              }
              return null;
          }
      };
  
      ProgressBar progressBar = new ProgressBar();
      progressBar.setProgress(0);
      progressBar.progressProperty().bind(task.progressProperty());
  
      Label labelCount = new Label();
      labelCount.textProperty().bind(task.messageProperty());
  
      final Label labelState = new Label();
  
      Button btnStart = new Button("Start Task");
      btnStart.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  
          @Override
          public void handle(ActionEvent t) {
  
               new Thread(task).start();
  
  
          }
      });
  
      Button btnCancel = new Button("Cancel Task");
  
      btnCancel.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  
          @Override
          public void handle(ActionEvent t) {
              task.cancel();
  
          }
      });
  
      Button btnReadTaskState = new Button("Read Task State");
      btnReadTaskState.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  
          @Override
          public void handle(ActionEvent t) {
              labelState.setText(task.getState().toString());
          }
      });
  
  
      VBox vBox = new VBox();
      vBox.setPadding(new Insets(5, 5, 5, 5));
      vBox.setSpacing(5);
      vBox.getChildren().addAll(
              progressBar, 
              labelCount,
              btnStart,
              btnCancel,
              btnReadTaskState, 
              labelState);
  
      StackPane root = new StackPane();
      root.getChildren().add(vBox);
  
      Scene scene = new Scene(root, 300, 250);
  
      primaryStage.setTitle("java-buddy.blogspot.com");
      primaryStage.setScene(scene);
      primaryStage.show();
  
  }
  
  public static void main(String[] args) {
      launch(args);
  }
  </code></pre>
  
  <p>}</p>