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

不问归期 提交于 2019-12-06 09:27:29

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