2013-02-21 2 views
4

У меня был код из stackoverflow на «контрольном контроллере доступа от родительского контроллера», как показано ниже.JavaFX 2.2 -fx: include - как получить доступ к родительскому контроллеру из дочернего контроллера

ParentController.java

public class ParentController implements Initializable{ 

    @FXML private childController childController; 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     childController.sessionLabel.setText("Real blabla"); 
     System.out.println("sessionLabel= " + childController.sessionLabel.getText()); 
    } 

} 

childController.java

public class childController implements Initializable{ 

    @FXML public Label sessionLabel; 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
    } 

} 

child.fxml

<AnchorPane maxHeight="20.0" prefWidth="600.0" 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="" /> 
     </HBox> 
    </children> 
</AnchorPane> 

parent.fxml

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="ParentController"> 
<children> 
    <fx:include fx:id="child" source="child.fxml"/> 
    <Label fx:id="lebelInParent" prefHeight="20.0" text="" /> 
</children> 
</AnchorPane> 

My Query - я хочу получить доступ к lebelInParent из parent.fxml из childController.java. Любая помощь будет назначена.

ответ

3

Я сделал, как следующий -

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> 

объяснений - он загружает parent.fxml и когда я нажимаю на sessionLabel, он вызывает метод mClicked из childController и child.getParent() .lookup, найдите идентификатор и возвращаемый узел.

Смежные вопросы