How to make child Tree Items checked/un-checked if parent Tree was made checked/un-checked

时间秒杀一切 提交于 2020-01-02 09:27:15

问题


I'm having a Check-box Tree View structure, consisting of parent and child nodes.

I want to make all the child nodes [of the parent node] appear as checked if parent Tree is checked. Similarly, if parent Tree is unchecked then its childrens should be unchecked.


回答1:


The best way to achieve this would be using the JFace CheckboxTreeViewer as it has the following predefined methods to simplify the task.

  1. setSubtreeChecked - Sets the child elements checked on selecting the parent node
  2. getCheckedElements - Gets all the checked tree elements

    final CheckboxTreeViewer treeViewer = new CheckboxTreeViewer(parent);
    
    // When user checks a checkbox in the tree, check all its children
    treeViewer.addCheckStateListener(new ICheckStateListener() {
    public void checkStateChanged(CheckStateChangedEvent event) {
        // If the item is checked . . .
        if (event.getChecked()) {
          // . . . check all its children
          treeViewer.setSubtreeChecked(event.getElement(), true);
        }
      }
    });
    
    // Get the selected elements from the tree
    Object[] actuallyChecked = treeViewer.getCheckedElements();
    


来源:https://stackoverflow.com/questions/25197047/how-to-make-child-tree-items-checked-un-checked-if-parent-tree-was-made-checked

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