WestCoastProjects
WestCoastProjects

Reputation: 63162

How to perform a longer running task requiring some GUI operations asynchronously in JavaFX?

We have a set of scenes for which the intent is to - at a certain point after it is "safe" to do so - save them to image files. "Safe" here means "after some backend work done and also after the scenes are fully painted/rendered."

The single-threaded nature of the FxApplication thread needs to be taken into account : all gui related work needs to happen on the FxApplication thread. So then it is not clear to me how to do arbitrary gui-related operations that require access to the Graphics Context.

Note the following question Platform.runLater and Task in JavaFX does not address this case because the GUI operation shown:

           bar.setProgress(counter/1000000.0);

is a "builtin". We need to do arbitrary gui operations.

..

So an outline of how to properly sequence the work would be appreciated.

Upvotes: 0

Views: 368

Answers (1)

WestCoastProjects
WestCoastProjects

Reputation: 63162

The following approach is working AFA running GUI operations in the "background" (there is only one GUI thread - so 'background' tasks for GUI operations is a relative term..)

update credit to @jewelsea for a significant chunk of the approach https://gist.github.com/jewelsea/5072743 "Render 300 charts off screen and save them to files in JavaFX."

  • Launch a block of code in Platform.runLater
  • In that block of code launch a Task
  • In the Task:
    • Do the off-thread backend non-gui work
    • When completed with the non-gui work:
    • launch a Platform.runLater that includes:
    • the GUI code you want to run after the backend operations were completed.

This approach is working: I can see the windows launched, then backend operations happening while the GUI is still responsive: and then eventually - once the backend operations are completed - the additional GUI operations are then performed.

Upvotes: 1

Related Questions