How to bind controls across two fxml from the Main controller

前端 未结 1 1991
一个人的身影
一个人的身影 2021-01-16 17:59

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

相关标签:
1条回答
  • 2021-01-16 18:28

    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)

    0 讨论(0)
提交回复
热议问题