0

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.

  • the javafx.application.Platform.runLater(runnable) is intended for short-lived operations. In fact that is how we launch the separate scenes: one scene for each invocation of runLater. That works fine.

  • the Task which is specifically intended for longer running operations. But how should the Task interoperate with the FxApplication thread? There does not seems to be any "queueing mechanism" for the Task to put work back into the FxApplication eventing loop.

    We should likely not attempt to perform GUI related ops from the Task - that would violate the FxApplication thread's responsibilities -and lead to race conditions.

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.

Community
  • 1
  • 1
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • Perhaps you are looking for something like: [Render 300 charts off screen and save them to files in JavaFX.](https://gist.github.com/jewelsea/5072743) – jewelsea Aug 10 '15 at 16:40
  • I did /have looked closely at that - and it influenced the approach in my answer. I will add the link to the answer. I had a separate issue that the rendering is still not working http://stackoverflow.com/questions/31909297/getting-black-image-when-saving-a-javafx-snapshot – WestCoastProjects Aug 10 '15 at 19:20

1 Answers1

1

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.

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560