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.fxm
I hope you could expose the TextField from the FXMLPrimaryController, and have a TextField property in the FXMLsecondController; then set that property when you assemble the pieces together. This would not be a good design, though, because you would be exposing details of the view through the controllers.
I am sure you don't want the TextArea but the data of the TextArea in second.fxml
. So what I would suggest is to expose a String property in each of the controllers and bind them together.
public class FXMLPrimaryController {
private ReadOnlyStringWrapper text ;
@FXML
private TextArea myArea;
public void initialize() {
text= new ReadOnlyStringWrapper(this, "text");
text.bind(myArea.textProperty());
}
public ReadOnlyStringProperty textProperty() {
return text.getReadOnlyProperty();
}
public String getText() {
return text.get();
}
@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();
}
}
and then in your FXMLsecondController, you can have
public class FXMLsecondController {
private StringProperty text ;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println(text.getValue());
}
public void initialize() {
text = new SimpleStringProperty(this, "text");
}
public StringProperty textProperty() {
return text ;
}
public String getText() {
return text.get();
}
}
Then you can bind these two, using something like this
FXMLLoader primaryLoader = new FXMLLoader(getClass().getResource("primary.fxml"));
Parent textAreaHolder = (Parent) primaryLoader.load();
FXMLLoader secondaryLoader = new FXMLLoader(getClass().getResource("second.fxml"));
Parent textAreaUser = (Parent) secondaryLoader.load();
FXMLPrimaryController primaryController = (FXMLPrimaryController) textAreaHolder.getController();
FXMLsecondController secondController = (FXMLsecondController) textAreaUser.getController();
secondController.textProperty().bind(primaryController.textProperty());
This now binds the variable text
of both the controllers and you can easily use the value that is entered in the textarea of the primary controller in your secondary controller !