title: Updating JavaFX GUI from a service thread
body: <p>How can I safely update the widgets on a JavaFX GUI from within a JavaFX Service.  I remember when I was developing with Swing, I used to 'invoke later' and other various swing worker utilities to ensure that all updates to the UI were handled safely in the Java Event Thread.  Here is an example of a simple service thread that handles datagram messages.  The bit that is missing is where the datagram message is parsed and corresponding UI widgets are updated.  As you can see the service class is very simplistic.  </p>
  
  <p>I'm not sure if I need to use simple binding properties (like message) or alternatively should I should pass widgets to the constructor of my StatusListenerService (which is probably not the best thing to do). Can someone give me a good similar example that I would work from.</p>
  
  <pre><code>public class StatusListenerService extends Service&lt;Void&gt; {
      private final int mPortNum;
  
      /**
       *
       * @param aPortNum server listen port for inbound status messages
       */
      public StatusListenerService(final int aPortNum) {
          this.mPortNum = aPortNum;
      }
  
      @Override
      protected Task&lt;Void&gt; createTask() {
          return new Task&lt;Void&gt;() {
              @Override
              protected Void call() throws Exception {
                  updateMessage("Running...");
                  try {
                      DatagramSocket serverSocket = new DatagramSocket(mPortNum);
                      // allocate space for received datagrams
                      byte[] bytes = new byte[512];
                      //message.setByteBuffer(ByteBuffer.wrap(bytes), 0);
                      DatagramPacket packet = new DatagramPacket(bytes, bytes.length);                    
                      while (!isCancelled()) {                    
                          serverSocket.receive(packet);
                          SystemStatusMessage message = new SystemStatusMessage();
                          message.setByteBuffer(ByteBuffer.wrap(bytes), 0);                         
                      }
                  } catch (Exception ex) {
                      System.out.println(ex.getMessage());
                  }
                  updateMessage("Cancelled");
                  return null;
              } 
          };
      }
  }
  </code></pre>