title: How can I externally update a JavaFX scene?
body: <p>I am trying to learn JavaFX and convert a swing application to JavaFX.
  What I want to do is use JavaFX to display the progress of a program.</p>
  
  <p>What I was previously doing in Swing was first creating a JFrame with a custom JComponent. Then have my main program call a method of the custom JComponent that would change the colour of a shape within the JComponent and repaint().</p>
  
  <p>The below gives an idea of the kind of thing I want to achieve in JavaFX:</p>
  
  <pre><code>//Run JavaFX in a new thread and continue with the main program.
  public class Test_Main{
      public static void main(String[] args) {
          Test test = new Test();
          Thread t = new Thread(test);
          t.start();
  
          //Main Program
          JOptionPane.showMessageDialog(null, "Click 'OK' to continue.",
                  "Pausing", JOptionPane.INFORMATION_MESSAGE);
  
          //Update Progress
          test.setText("Hello World!");
      }    
  }
  </code></pre>
  
  <p>I currently have this as my runnable.</p>
  
  <pre><code>public class Test extends Application implements Runnable{
      Button btn;
  
      @Override
      public void run() {
          launch();
      }
  
      @Override
      public void start(Stage stage) throws Exception {
          StackPane stack = new StackPane();
          btn = new Button();
          btn.setText("Testing");
          stack.getChildren().add(btn);
          Scene scene = new Scene(stack, 300, 250);
          stage.setTitle("Welcome to JavaFX!");
          stage.setScene(scene);
          stage.show();        
      }    
  
      public void setText(String newText){
          btn.setText(newText);
      }
  }
  </code></pre>
  
  <p>Everything runs fine until I try to update the text of the button in which I get a <code>NullPointerException</code>. I guess this has something to do with the JavaFX application thread. I cannot find anything on the net though which describes how to update things externally.</p>
  
  <p>I see a lot of mention about <code>Platform.runLater</code> and <code>Task</code> but these are usually nested in the start method and run on timers.</p>
  
  <p><strong>UPDATE:</strong>
  Just to clarify I am hoping to achieve something like this:</p>
  
  <pre><code>public class Test_Main{
      public static void main(String[] args) {
          final boolean displayProgress = Boolean.parseBoolean(args[0]);
  
          Test test = null;
          if(displayProgress){    //only create JavaFX application if necessary
              test = new Test();
              Thread t = new Thread(test);
              t.start();
          }
  
          //main program starts here
  
          // ...
  
          //main program occasionally updates JavaFX display
          if(displayProgress){    //only update JavaFX if created
              test.setText("Hello World!");
          }
  
          // ...
  
          //main program ends here
      }    
  }
  </code></pre>