user2799603
user2799603

Reputation: 903

How can I run javafx components with Java thread?

How can I run javafx components with Java thread from the run() method? Is it even supported at all? Thanks!

Upvotes: 0

Views: 142

Answers (1)

An SO User
An SO User

Reputation: 24998

Modifying a live scene from any thread other than Application thread is not allowed. If you want to do that from a java.lang.Thread then somewhere in your implementation of the run() you need this:

Platform.runLater(new Runnable(){
  @Override
  public void run(){
    // change your scene graph here
  }
});  

That causes all your changes to happen on the Application thread. If you have a task that is to execute repeatedly, have a look at javafx.concurrent.Service<V>. The docs say:

As part of the JavaFX UI library, the Service knows about the JavaFX Application thread and is designed to relieve the application developer from the burden of manging multithreaded code that interacts with the user interface.

Upvotes: 1

Related Questions