我有以上层次结构,其中FXML-B位于FXML-A之上,使用" parent_stackPane从Controller-A加载。 getChildren()。setAll (child_fxmlLoader_load)"一种方式。因此FXML-A是FXML-B的父母。
无论如何,我可以从子控制器B更改父FXML-A中标签的文本吗?
答案 0 :(得分:0)
在StringProperty
中定义ControllerB
:
public class ControllerB {
private StringProperty text = new SimpleStringProperty();
public StringProperty textProperty() {
return text ;
}
public final String getText() {
return textProperty().get();
}
public final void setText(String text) {
textProperty().set(text);
}
// other code as before ...
}
在ControllerA
中加载第二个fxml时,将标签文本绑定到textProperty
:
public class ControllerA {
@FXML
private Label label ;
@FXML
private StackPane parentStackPane ;
@FXML
private void someHandlerMethod() throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("FxmlFileB.fxml"));
Parent rootB = loader.load();
ControllerB controllerB = loader.getController();
label.textProperty().unbind();
label.textProperty().bind(controllerB.textProperty());
parentStackPane.getChildren().setAll(rootB);
}
// other code as before...
}
现在,当您在setText(...)
中致电ControllerB
时,它会更新标签中的文字。