[英]JavaFX - Access parent fx:id from child
Let's say I have a button in a nested (child) fxml file, and in the child's controller I have created an action event that fires on button click. 假设我在嵌套(子)fxml文件中有一个按钮,在子控制器中我创建了一个按钮点击时触发的动作事件。 From that method I want to disable or enable certain controls (for instance some tabs in a tabpane) in my main (parent) fxml. 从该方法我想在我的主(父)fxml中禁用或启用某些控件(例如tabpane中的一些选项卡)。
How can I achieve this? 我怎样才能做到这一点?
This is the closest thread I found, which discussed how to do it the other way around: JavaFX - Access fx:id from nested FXML 这是我找到的最接近的线程,它讨论了如何反过来 : JavaFX - 从嵌套的FXML访问fx:id
Any help is greatly appreciated! 任何帮助是极大的赞赏!
Define an observable property in the nested controller, and observe it from the surrounding controller: 在嵌套控制器中定义一个observable属性,并从周围的控制器中观察它:
public class ChildController {
private final BooleanProperty stuffShouldBeDisabled = new SimpleBooleanProperty();
public BooleanProperty stuffShouldBeDisabledProperty() {
return stuffShouldBeDisabled ;
}
public final boolean getStuffShouldBeDisabled() {
return stuffShouldBeDisabledProperty().get();
}
@FXML
private void handleButtonClick(ActionEvent event) {
stuffShouldBeDisabled.set( ! stufShouldBeDisabled.get() );
}
// ...
}
and then in the "surrounding" (Parent) controller (ie the controller for the FXML file with the <fx:include>
tag): 然后在“周围”(Parent)控制器(即带有<fx:include>
标签的FXML文件的控制器)中:
public class MainController {
@FXML
private ChildController childController ; // injected via <fx:include fx:id="child" ... />
@FXML
private Tab someTab ;
public void initialize() {
childController.stuffShouldBeDisabledProperty().addListener((obs, wasDisabled, isNowDisabled) -> {
someTab.setDisable(isNowDisabled);
}
}
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.