using JavaFX for an application and I have a Main.fxml file with some fxml child files inside it.
I would like to access to MainController class of Main.fxml from the child Controllers.
I'll try to explain better with an example:
MainFxml:
<HBox fx:controller="MainController.java">
<fx:include source="child.fxml"/>
</HBox>
MainController:
public class MainController implements Initializable {
private String string;
public void setString (String string) {
this.string = string;
}
ChildFxml:
<HBox fx:id="child" fx:controller="ChildController.java">
<Button text="hello" onAction="#selectButton"></Button>
</HBox>
ChildController:
public class ChildController implements Initializable {
@FXML HBox child;
@FXML Button button;
@FXML
public void selectButton (ActionEvent event) {
// here call MainController.setString("hello");
}
I tried this solution found on StackOverflow but I need to get the Controller reference of the Main.fxml that has been already loaded. Is there any method to get the Controller starting from a specific Pane? Something like:
// child.getParent().getController();
StringProperty
object betweenChildController
andMainController
so MainController can update itself when the string changes. See #31266998 for some more info. – Alsace