I have a JavaFX / Java 8 application written with NetBeans 8 (no SceneBuilder
).
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 TextArea
. Some of the methods in FXMLPrimaryController.java are about appending to that TextArea
.
This application now spawns a second window (another "stage") with its own FXML (second.fxml) and its own controller class (FXMLsecondController.java).
Within the second controller class, how can I access the TextArea in the primary?
Here's a sample of the relevant code:
primary.fxml:
<Button text="press me!" onAction="#openSecondWindow" />
<TextArea fx:id="myArea" />
FXMLPrimaryController.java:
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();
}
}
There is nothing fancy about second.fxml. Assume there is a button with onAction="#writeSomething"
.
In FXMLsecondController.java, I would like a function that references the above TextArea
.