I Need to bind controls from different fxml in the main controller. I have 3 fxml files named MainView.fxml, ChildView1.fxml and ChildView2.fxml.
MainView.fx
You can inject the controllers of the included fxml on the MainViewController
controller, as indicated here.
Then, you can perform the desired bindings at MainViewController
's initialize method. Considering that the child controllers expose the Button
and the TableView
with getter
methods, the MainViewController
could be:
public class MainViewController {
@FXML
private ChildView1Controller childView1Controller;
@FXML
private ChildView2Controller childView2Controller;
@FXML
public void initialize() {
childView1Controller.getButton1().visibleProperty().bind(
childView2Controller.getTableView().getSelectionModel().selectedItemProperty().isNotNull());
}
}
To correctly inject these child controllers, it is needed to change the include elements at MainView.fxml, updating their fx:id
attribute:
<fx:include fx:id="childView1" source="ChildView1.fxml" />
<fx:include fx:id="childView2" source="ChildView2.fxml" />
Relevant comment from @Dil:
The requirement is that if the fx:id of your included resource is "xxx", the variable name for the corresponding controller is xxxController (so details -> detailsController)