JavaFX 2.2 -fx:include - how to access parent controller from child controller

前端 未结 1 1280
南笙
南笙 2021-01-18 13:47

I had code from stackoverflow on \"access child controller from parent controller\" as below.

ParentController.java

public class ParentController  i         


        
相关标签:
1条回答
  • 2021-01-18 14:46

    I did as following -

    public class childController  implements Initializable{
    
        @FXML public Label sessionLabel;
        @FXML private AnchorPane child;
    
        @Override
        public void initialize(URL location, ResourceBundle resources) {
        }
        @FXML
        private void mClicked (){
            System.out.println(child.getParent().lookup("#lebelInParent"));
        }
    }
    

    child.fxml

    <AnchorPane fx:id="child" xmlns:fx="http://javafx.com/fxml" fx:controller="childController">
       <children>
          <HBox id="hbox_top" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
             <Label fx:id="sessionLabel" prefHeight="20.0" text="" onMouseClicked="#mClicked"/>  
          </HBox>
       </children>
    </AnchorPane>
    

    explaination - it loads parent.fxml and when I click on sessionLabel, it calls mClicked method of childController and child.getParent().lookup, search for Id and return Node.

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