1 Reply Latest reply: Oct 22, 2012 2:27 PM by jsmith RSS

    Using JavaFX2 to visualise a background process

    969485
      I have a simple conveyor simulation that I am using to develop more efficient conveyor routing algorithms for a manufacturer. I want JavaFX to be able to display what is happening in a background thread. Obviously I can use Platform.runLater to update the GUI. My question simply is: is there an accepted best practice for the aforementioned application?
        • 1. Re: Using JavaFX2 to visualise a background process
          jsmith
          Using Platform.runLater to visualise your background process is fine.

          The couple of things you need to be careful of are:
          1. Never read or write the active scenegraph objects or trigger a bind change on those objects from your background process.
          2. Don't call Platform.runLater so often that you end up overloading the JavaFX event system.

          You might also want to take a look at the Task and Service interfaces. These encapsulate background processes to provide APIs for threadsafe feedback of messages, progress and return values. Here is a sample of using a Task: https://gist.github.com/2305098 and another one of using a service: https://gist.github.com/2774476.

          The Task and Service javadoc has lots of good (though incomplete) examples:
          http://docs.oracle.com/javafx/2/api/javafx/concurrent/Task.html
          http://docs.oracle.com/javafx/2/api/javafx/concurrent/Service.html

          The JavaFX documentation provides some background info on when you might want to use the different concurrency options provided by JavaFX:
          http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm

          In general if the task is not really managed by or kicked off from the UI, then Platform.runLater is probably the way to go (e.g. responding to a push message from a server such as a temperature update from a sensor), otherwise use the Task and Service interfaces (e.g. if the user clicked on a button to initiate a call/response message to a server such as initiating a google search and collecting the search response).