Irakli Kobalava
Irakli Kobalava

Reputation: 21

Updating javaFX browser Content from background thread

I have Javafx project with a browser inside. I am using angularJS within a browser. When I try to go to a new state from a background thread the view does not do it until I interact with one of the buttons. So the angular code is called but the view is frozen until clicking on something.

Task task = new Task<Void>() {
        @Override public Void call() {
            while (true) {
                try {
                    System.out.println("sLEEPING");
                    Thread.sleep(4000);
                    System.out.println("Reloading page");
                    Platform.runLater(() -> {
                        communicator.changeUI();

                    });
                    System.out.println("pAGE Reloaded");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    Thread t = new Thread(task);
    t.start();

Upvotes: 0

Views: 172

Answers (1)

James_D
James_D

Reputation: 209428

Don't update the state of the UI from a background thread (see the "Threading" section in the documentation). Wrap the code that actually changes the UI in Platform.runLater() (or use a Task and an onSucceeded handler).

E.g.:

// Runs in some background thread:
public class MyRunnable {

    @Override
    public void run() {
        // long-running code here...

        // to update UI:
        Platform.runLater(() -> {
            // update UI here...
        });
    }
}

Upvotes: 1

Related Questions