检查或取消某些项目时如何检查和取消选中所有项目

时间:2016-12-19 19:48:51

标签: javafx-2 javafx-8 controlsfx

JavaFX CheckComboBox 检查或取消确认某些项目时如何检查和取消选中所有项目。

Check all when ckeck item All

Uncheck all when unckeck item All

ObservableList<String> list = FXCollections.observableArrayList("All","Item 1", "Item 2", "Item 3", "Item 4");

checkBoxCombo.getItems().addAll(list);  

checkBoxCombo.getCheckModel().getCheckedItems().addListener(new ListChangeListener<String>() {      

public void onChanged(ListChangeListener.Change<? extends String> c) {

            if(checkBoxCombo.getCheckModel().isChecked(0))                  
                    checkBoxCombo.getCheckModel().checkAll();                
             }
         });

1 个答案:

答案 0 :(得分:3)

向侦听器添加boolean字段以防止递归:

checkBoxCombo.getCheckModel().getCheckedItems().addListener(new ListChangeListener<String>() {

    private boolean changing = false;

    @Override
    public void onChanged(ListChangeListener.Change<? extends String> c) {
        if (!changing && checkBoxCombo.getCheckModel().isChecked(0)) {
            // trigger no more calls to checkAll when the selected items are modified by checkAll
            changing = true;
            checkBoxCombo.getCheckModel().checkAll();
            changing = false;
        }
    }
});