Rendering JavaFX 2 charts in background

后端 未结 3 1087
独厮守ぢ
独厮守ぢ 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: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

提交回复
热议问题