0

Like in subject: why Task stops when I try to add children to Pane? How can I solve this problem without leaving behind Task? Below working example:

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;

public class TEST extends Application {

    private Pane displayPane;

    public Parent createContent() {

        /* layout */
        BorderPane layout = new BorderPane();

        /* layout -> center */
        displayPane = new Pane();
        displayPane.setMinSize(200, 200);
        displayPane.setMaxSize(200, 200);

        /* return layout */
        layout.setCenter(displayPane);

        return layout;
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene(new Scene(createContent()));
        stage.show();

        startTask();
    }

    public void startTask() {
        Task task = new Task<Void>() {
            @Override
            public Void call() {

                // This works fine.
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }


                for (int i = 0; i < 5; i++) {
                    System.out.println("Iteration no.: " + i);
                    Ellipse ellipse = new Ellipse(100, 100, 10, 10);
                    ellipse.setFill(Color.BLACK);

                    // Here task stops on iteration np.: 0.
                    displayPane.getChildren().add(ellipse);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                    displayPane.getChildren().remove(displayPane.getChildren().size() - 1);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
                return null;
            }
        };
        new Thread(task).start();
    }
}
bluevoxel
  • 4,978
  • 11
  • 45
  • 63
  • 1
    It stops because it throws an exception, which you don't catch (or process in any other way, such as `task.setOnFailed(...)`. – James_D Jun 12 '15 at 14:05
  • I wrapped `displayPane.method()` lines between `Platform.runLater()` blocks and everything now is just fine. Thank you. – bluevoxel Jun 12 '15 at 14:07
  • It would actually be much easier to use a `Timeline` for this kind of functionality. See, e.g. http://stackoverflow.com/questions/9966136/javafx-periodic-background-task – James_D Jun 12 '15 at 14:08
  • I used `TimeLine` for such functionality before, but I need a solution, which allows for providing answers to the displaying stimuli via e.g keyboard with least possible latencies resulting in this case from single-threading. – bluevoxel Jun 12 '15 at 14:19

0 Answers0