gpt4 book ai didi

JavaFX - 访问父 fx :id from child

转载 作者:太空宇宙 更新时间:2023-11-04 11:42:06 24 4
gpt4 key购买 nike

假设我在嵌套(子)fxml 文件中有一个按钮,并且在子 Controller 中我创建了一个在单击按钮时触发的操作事件。通过该方法,我想在我的主(父)fxml 中禁用或启用某些控件(例如选项卡中的某些选项卡)。

我怎样才能实现这个目标?

这是我找到的最接近的线程,它讨论了如何以相反的方式做到这一点:JavaFX - Access fx:id from nested FXML

非常感谢任何帮助!

最佳答案

在嵌套 Controller 中定义一个可观察的属性,并从周围的 Controller 中观察它:

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() );
}

// ...
}

然后在“周围”(父) Controller (即带有 <fx:include> 标签的 FXML 文件的 Controller )中:

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);
}
}

// ...
}

关于JavaFX - 访问父 fx :id from child,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42696623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com