javafx, update ui from another thread

AgostinoX picture AgostinoX · Jul 27, 2012 · Viewed 41.7k times · Source

I have a javafx application, and a worker thread, implemented via javafx.concurrent.Task, that performs a long process, that is zipping and uploading a set of files.
I've connected the task progress to a progress bar via progressProperty.
In addition to this i want a detailed state about the item being processed to be reported into the ui. That is, the name of the file being processed along with its size and any error that may arise from the single file process.
Updating the UI with these information cannot be done from the worker thread, at the most i can add it to a synchronized collection.
But then i need some event to inform the UI that new data is available.
Does javafx have some specific support for this issue?

update, better formulation

Instead of designing an ad hoc cross-thread mechanism as Platform.runLater, i'm trying to allow each property to be listened from other threads. Just like runningProperty and stateProperty provided by Task

Answer

Daniel B. Chapman picture Daniel B. Chapman · Jul 27, 2012

I'm running into a similar issue, as far as I can tell you have to deal with the error handling yourself. My solution is to update the UI via a method call:

Something like:

  try
  {
    //blah...
  }
  catch (Exception e)
  {
    reportAndLogException(e);
  }
  ...
  public void reportAndLogException(final Throwable t)
  {
    Platform.runLater(new Runnable() {
      @Override public void run() {
        //Update UI here     
      }
    });
  }

Essentially I am just manually moving it back to the UI Thread for an update (as I would do in pretty much any other framework).