Display JavaFX 2 diagrams in the background

I have an application that is used to analyze data. Most of the application is the ability to display charts based on the collected data and be able to export a large number of tasks in one batch operation. So far I have used JFreeChart, but I would like to use native JavaFX charts so that the exported charts still look the same as on the screen in the application.

I am on JavaFX 2.2.1 (jdk 7u6).

I can generate diagrams in one batch, but this means that I have to freeze the user interface (UI), since these diagrams should be displayed in the JavaFX Application Thread application. I am using Platform.runLater (new Runnable () {...}); a command for this, wrapped around the code that generates the diagrams.

If instead I complete each individual charting in Platform.runLater (new Runnable () {...}); The GUI does not freeze as before, but I also do not get feedback, because I canโ€™t detect when each of the individual diagrams is generated (they start at a later stage, and I canโ€™t control when this can happen, and, as far as I know there is no callback).

For this particular event, I would like to show a progress bar for the user, and I want this progress bar to be updated along with the actual charting.

Any suggestions or tips on how this can be achieved?

+6
source share
3 answers

This issue has been resolved in the Oracle JavaFX forum thread : Render graphics in the background .

The solution was to have a SaveChartsTask with three subtasks:

  • Creating diagrams on the scene graph off-screen.
  • Take snapshots of each screen diagram on a JavaFX image.
  • Use imageio procedures to export JavaFX images to files.

WorkDone properties in tasks allowed controlling the process of operations. By disassembling the general work into subtasks, while maintaining work with the JavaFX application flow, when possible, and using the CountDownLatches and BlockingQueues needed to synchronize operations, the user interface could remain responsive.

+5
source

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

Your problem is easily solved by using the Task Object for loading diagrams separately (for example, one task per diagram). Quoting the tutorial page:

Tasks are used to implement the logic of work that must be performed in the background thread.

Since Task implements the Work interface, you can use your Tasks to examine their Worker.State . Guideline Note:

A reusable worker will transition from CANCELED, SUCCEEDED, or FAILED back to READY.

This will solve your feedback problem, as you can always find out if it still works or not, because the Worker.State object has the following possible states:

  • CANCEL
  • FAILED
  • READY
  • RUNNING
  • SCHEDULE
  • SUCCEEDED

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

EDIT

Responding to your comment:

The problem is that the work performed inside the task in this case when generating the JavaFX diagram, and this code must be executed inside the JavaFX application thread

From the textbook:

Instead, use the JavaFX APIs provided by the javafx.concurrent package, which takes care of multi-threaded code that interacts with the user interface and ensures that this interaction occurs in the correct thread.

This means that the code that runs inside the Task object is already running in the JavaFX thread.

Hope this helps. Greetings

+1
source

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

0
source

All Articles