如果父树已被选中/未选中,如何使子树项目被选中/未选中 [英] How to make child Tree Items checked/un-checked if parent Tree was made checked/un-checked

查看:100
本文介绍了如果父树已被选中/未选中,如何使子树项目被选中/未选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由父级和子级节点组成的复选框树视图结构。

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.

推荐答案

最好的方法是使用JFace CheckboxTreeViewer ,因为它具有以下预定义的方法来简化任务。

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 -设置选择父节点时选中的子元素

  2. getCheckedElements -获取所有选中的元素树元素

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


这篇关于如果父树已被选中/未选中,如何使子树项目被选中/未选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆