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
This question was solved in the Oracle JavaFX forum thread: Render charts in background.
The solution was to have a SaveChartsTask with three subtasks:
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.
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:
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
To run a JavaFX Task
on the JFX thread, you can schedule the task to be run with Platform.runLater(myJFXTask);
.