问题
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.
setSubtreeChecked
- Sets the child elements checked on selecting the parent nodegetCheckedElements
- Gets all the checked tree elementsfinal 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