How to check and uncheck all items when checking or unckeck some of the items

蓝咒 提交于 2019-12-11 00:34:16

问题


JavaFX CheckComboBox How to check and uncheck all items when checking or unckeck some of the items.

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:


Add a boolean field to the listener to prevent recursion:

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


来源:https://stackoverflow.com/questions/41229964/how-to-check-and-uncheck-all-items-when-checking-or-unckeck-some-of-the-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!