Rendering JavaFX 2 charts in background

后端 未结 3 1082
独厮守ぢ
独厮守ぢ 2021-01-05 13:36

I have an application that is used for data analysis. A big part of the application is the ability to be able to show charts based on the data assembled, and to be able to e

相关标签:
3条回答
  • 2021-01-05 13:41

    This question was solved in the Oracle JavaFX forum thread: Render charts in background.

    The solution was to have a SaveChartsTask with three subtasks:

    1. Create charts in an off screen scene graph.
    2. Take snapshots of each off screen chart to an JavaFX image.
    3. Use imageio routines to export the JavaFX images to files.

    The workDone properties on tasks allowed the process of the operations to be monitored. By splitting the total work up into subtasks, keeping work off of the JavaFX application thread when possible and making use of CountDownLatches and BlockingQueues as necessary to synchronize operations, the UI was able to remain responsive.

    0 讨论(0)
  • 2021-01-05 13:42

    Since it seems that you are new to JavaFX-2. I recommend you to read the JavaFX-2 Concurrency article from oracle documentation.

    Your problem is easily solved by using the Task Object to load your charts individually (One Task per Chart for example). Quoting the tutorial page:

    Tasks are used to implement the logic of work that needs to be done on a background thread.

    Since Task implements the Work Interface, you can use your Tasks to probe for their Worker.State. Quoting the manual:

    A reusable Worker will transition from CANCELLED, SUCCEEDED or FAILED back to READY.

    This would solve your problem about feedback since you'll always be able to know if a Task is still Running or not since the Worker.State object has the following possible States:

    • CANCELLED
    • FAILED
    • READY
    • RUNNING
    • SCHEDULED
    • SUCCEEDED

    As for your progress bar, you can use the updateProgress(double done, double max) to set your Task progress and then simply set the progress of your progress bar by binding the ProgressBar.progressProperty() to Task.progressProperty().

    EDIT

    Answering to your comment:

    The problem is that the work performed inside the Task in this case in the generation of the JavaFX Chart, and that code needs to be executed inside the JavaFX Application Thread

    From the tutorial:

    Instead, use the JavaFX APIs provided by the javafx.concurrent package, which takes care of multithreaded code that interacts with the UI and ensures that this interaction happens on the correct thread.

    Which means the code executed inside the Task object is already being executed in the JavaFX Thread.

    I hope it helped. Cheers

    0 讨论(0)
  • 2021-01-05 13:52

    To run a JavaFX Task on the JFX thread, you can schedule the task to be run with Platform.runLater(myJFXTask);.

    0 讨论(0)
提交回复
热议问题