@RunOnFxThread
@RunOnFxThread annotation offers for a convenient way to execute the target on JavaFX UI thread (JavaFX Application Thread).
Annotation can be applied at method level or at class level (to mark all methods within a class)
Example
private final VBox vBox = new VBox();
public void start(@Observes FxPostStartupEvent event) {
Stage = event.getPrimaryStage();
Scene scene = new Scene(this.vBox);
stage.setScene(scene);
stage.show();
try (ExecutorService executorService = Executors.newSingleThreadExecutor()) {
executorService.submit(() -> this.showNode(this.createExpensiveNode()));
}
}
Node createExpensiveNode() { (1)
return new Label(LocalTime.now().toString());
}
@RunOnFxThread
void showNode(Node node) { (2)
this.vBox.getChildren().add(nod
| 1 | createExpensiveNode method will be executed in a thread from executor service |
| 2 | showNode method will be executed on JavaFX UI thread (JavaFX Application Thread) |