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