JavaFX How to - Submit the task to be run on the JavaFX Aplication Thread








Question

We would like to know how to submit the task to be run on the JavaFX Aplication Thread.

Answer

/*  w ww .  jav a2 s. c o  m*/
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() {
        System.out.println("init(): " + Thread.currentThread().getName());

        Runnable task = () -> System.out.println("Running the task on the "
                + Thread.currentThread().getName());

        // Submit the task to be run on the JavaFX Aplication Thread
        Platform.runLater(task);
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene(new Scene(new Group(), 400, 100));
        stage.setTitle("Using Platform.runLater() Method");
        stage.show();
    }
}