Wild_Wind Wild_Wind - 2 years ago 215
Java Question

JavaFX Updating UI from another thread

I have a main class

public class Main {
public static void main(String[] args) {
View view = new View();
Platform.runLater(() -> view.changeTitle());

and a view JavaFX class

public class View extends Application {

Stage primaryStage;

public View() {


public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
this.primaryStage = primaryStage;

public void changeTitle() {

I want the main class to do something in JavaFX thread but my code doesn't work. In documentation said that I can call Platform.runLater() from any thread I want. If I call Platform.runLater() from JavaFX thread (in start(), for example), everything is OK.

Answer Source

First, main(String) is blocked on Application.launch(Class).

From Oracle Javadocs:

The launch method does not return until the application has exited, either via a call to Platform.exit or all of the application windows have been closed.

Second, you're creating a new View instance on the second line. That will NOT be same instance Application created, so your Platform.runLater wouldn't affect the launched application even if that code were reachable before it exited.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download