JavaFX 2.2 -fx:include - 如何从子控制器访问父控制器 [英] JavaFX 2.2 -fx:include - how to access parent controller from child controller
问题描述
我有来自stackoverflow的代码从从父控制器访问子控制器如下。
ParentController.java
public class ParentController implements可初始化{
@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可初始化{
@FXML public Label sessionLabel;
@Override
public void initialize(URL location,ResourceBundle resources){
}
}
child.fxml
< AnchorPane maxHeight = 20.0prefWidth =600.0xmlns:fx =http://javafx.com/fxmlfx:controller =childController>
< children>
< HBox id =hbox_topAnchorPane.leftAnchor =0.0AnchorPane.rightAnchor =0.0>
< Label fx:id =sessionLabelprefHeight =20.0text =/>
< / HBox>
< / children>
< / AnchorPane>
parent.fxml
< AnchorPane id =AnchorPaneprefHeight =400.0prefWidth =600.0xmlns:fx =http://javafx.com/fxmlfx:controller =ParentController>
< children>
< fx:include fx:id =childsource =child.fxml/>
< Label fx:id =lebelInParentprefHeight =20.0text =/>
< / children>
< / AnchorPane>
我的查询 - 我想从childController.java访问parent.fxml的lebelInParent。
<$>解决方案p $ p>
public class childController implements可初始化{
@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 =childxmlns:fx =http://javafx.com/fxmlfx:controller =childController>
< children>
< HBox id =hbox_topAnchorPane.leftAnchor =0.0AnchorPane.rightAnchor =0.0>
< Label fx:id =sessionLabelprefHeight =20.0text =onMouseClicked =#mClicked/>
< / HBox>
< / children>
< / AnchorPane>
解释 - 它加载parent.fxml,当我点击sessionLabel时,它调用childController的mClicked方法, child.getParent()。lookup,搜索Id并返回Node。
I had code from stackoverflow on "access child controller from parent controller" as below.
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 - I want to access lebelInParent of parent.fxml from childController.java. Any help will be appriciated.
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.
这篇关于JavaFX 2.2 -fx:include - 如何从子控制器访问父控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!