title: How do I access a UI element from another controller class in JavaFX?
body: <p>I have a JavaFX / Java 8 application written with NetBeans 8 (no <code>SceneBuilder</code>).</p>
  
  <p>My application has a main window that has its own FXML file (primary.fxml) and its own controller class (FXMLPrimaryController.java).  One of the items in the FXML is a <code>TextArea</code>.  Some of the methods in FXMLPrimaryController.java are about appending to that <code>TextArea</code>.</p>
  
  <p>This application now spawns a second window (another "stage") with its own FXML (second.fxml) and its own controller class (FXMLsecondController.java).</p>
  
  <p>Within the second controller class, how can I access the TextArea in the primary?</p>
  
  <p>Here's a sample of the relevant code:</p>
  
  <p>primary.fxml:</p>
  
  <pre><code>&lt;Button text="press me!" onAction="#openSecondWindow" /&gt;
  &lt;TextArea fx:id="myArea" /&gt;
  </code></pre>
  
  <p>FXMLPrimaryController.java:</p>
  
  <pre><code>public class FXMLPrimaryController implements Initializable {
  
      @Override
      public void initialize(URL url, ResourceBundle rb) {
      }
  
      @FXML private TextArea myArea;
  
      final public void writeToTextArea() {
          myArea.appendText("hi!");
      }
  
      @FXML
      private void openSecondWindow(ActionEvent event) throws Exception {
  
          Group root = new Group();
          Stage stage = new Stage();
  
          AnchorPane frame = FXMLLoader.load(getClass().getResource("second.fxml"));
          root.getChildren().add(frame);
          Scene scene = new Scene(root);
  
          stage.setScene(scene);
          stage.show();
      }
  
  }
  </code></pre>
  
  <p>There is nothing fancy about second.fxml.  Assume there is a button with <code>onAction="#writeSomething"</code>.</p>
  
  <p>In FXMLsecondController.java, I would like a function that references the above <code>TextArea</code>.</p>