【问题标题】:Set the default selected checkboxes in a ControlsFX CheckComboBox在 ControlsFX CheckComboBox 中设置默认选中的复选框
【发布时间】:2019-07-27 14:14:10
【问题描述】:

我正在尝试使用Java 8ControlsFX CheckComboBox v8.0.3 设置默认选定值

我已经在this question 中尝试了接受的答案,但是当我尝试时

CheckComboBox.getCheckModel().check(0);        

我得到一个类不能从静态上下文中引用。当我尝试以下操作时,我无法解析方法 check(int);

final CheckComboBox<String> checkComboBox = new CheckComboBox<>(strings);
checkComboBox.getCheckModel().check(0);  

如果我尝试getCheckmodel().selectIndices(0),则仅当从CheckComboBox 中选择新值时,第一个All1 才会填充到CheckComboBox。选择索引后有没有办法刷新comboxBox 或任何其他方式来实现我想要的?

Text numberTypeText = new Text("Features supported:");
final ObservableList<String> strings = FXCollections.observableArrayList();
strings.add("All1");
strings.add("All2");
strings.add("All3");
strings.add("All4");
strings.add("All5");
strings.add("All6");
strings.add("All7");
strings.add("All8");

final CheckComboBox<String> checkComboBox = new CheckComboBox<>(strings);
        checkComboBox.getCheckModel().selectIndices(0);

checkComboBox.getCheckModel().getSelectedItems().addListener(new ListChangeListener<String>() {
    public void onChanged(ListChangeListener.Change<? extends String> c) {
        System.out.println(checkComboBox.getCheckModel().getSelectedItems());
    }
});

任何帮助将不胜感激。

【问题讨论】:

    标签: java javafx controlsfx


    【解决方案1】:

    当我尝试以下操作时,我无法解析方法 check(int);

    这是因为您使用的是非常旧的 ControlsFX 版本。将您的 ControlsFX 升级到更新版本。

    如果我尝试 getCheckmodel().selectIndices(0),则仅当从 CheckComboBox 中选择新值时,第一个 All1 才会填充到 CheckComboBox。

    这似乎是在较新版本的 ControlsFX 中修复的错误。

    在 v8.40.14 中,我可以使用 check() 方法设置默认复选框:

    checkComboBox.getCheckModel().check(0);
    

    或者,如果你想检查多个项目,checkIndices() 方法:

    checkComboBox.getCheckModel().checkIndices(0, 1);
    

    【讨论】:

    • 谢谢,成功了。我还必须将 Java 更新到 8u40 版
    猜你喜欢
    • 2021-04-26
    • 2018-05-28
    • 2015-11-17
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    相关资源
    最近更新 更多