3

I'm a complete noob with JavaFX 2 - (started this morning with a HelloWorld that I'm adapting).

I need to update a Text node with the result of a background thread. Is there anything special I need to be aware of with JavaFX2 or is it simply enough to submit a callable and update the text. A pointer to a tutorial would be appreciated.

user497087
  • 1,561
  • 3
  • 24
  • 41

2 Answers2

4

After calculating the results, running the

      Platform.runLater(new Runnable() {
            @Override
            public void run() {
                // Update the text node with calculated results
            }
       });

at the end of the same background thread is enough in normal situations. This link also maybe helpful:
Execute task in background in JavaFX.

Community
  • 1
  • 1
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
2

Platform.runLater() will run later on the JavaFX application thread - which is fine if the executed content is a quick running task (e.g. inexpensive computation without I/O or just a call to update the UI). Use a Timeline for animation or timer based things. Otherwise a Task or Service based solution, for which there is a tutorial. Don't ever read or write from objects involved in an active scenegraph (even updates triggered by binds) off of the JavaFX application thread. Some further discussion and examples are in this forum thread.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
jewelsea
  • 150,031
  • 14
  • 366
  • 406