我正在开发一个IntelliJ插件弹出对话框来检查来自CheckboxTree的值。为此,我将遵循以下问题:
Java Swing: Need a good quality developed JTree with checkboxes
但是,当我单击单个子节点时,父节点也将被选中。但是,只有当父节点的所有子节点都被选中时,才选择父节点,否则将取消选择。
发布于 2015-07-21 08:07:29
只需添加以下代码
在updatePredecessorsWithCheckMode函数中注释如下代码,并在for循环后添加代码
// If at least one child is selected, selecting also the parent
// if (childCheckedNode.isSelected) {
// parentCheckedNode.isSelected = true;
// }
}
//check the parent if all children are selected
if (parentCheckedNode.allChildrenSelected) {
parentCheckedNode.isSelected = true;
}全功能可供参考
// When a node is checked/unchecked, updating the states of the predecessors
protected void updatePredecessorsWithCheckMode(TreePath tp, boolean check) {
TreePath parentPath = tp.getParentPath();
// If it is the root, stop the recursive calls and return
if (parentPath == null) {
return;
}
CheckedNode parentCheckedNode = nodesCheckingState.get(parentPath);
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) parentPath.getLastPathComponent();
parentCheckedNode.allChildrenSelected = true;
parentCheckedNode.isSelected = false;
for (int i = 0; i < parentNode.getChildCount(); i++) {
TreePath childPath = parentPath.pathByAddingChild(parentNode.getChildAt(i));
CheckedNode childCheckedNode = nodesCheckingState.get(childPath);
// It is enough that even one subtree is not fully selected
// to determine that the parent is not fully selected
if (!childCheckedNode.allChildrenSelected) {
parentCheckedNode.allChildrenSelected = false;
}
// If at least one child is selected, selecting also the parent
// if (childCheckedNode.isSelected) {
// parentCheckedNode.isSelected = true;
// }
}
//check the parent if all children are selected
if (parentCheckedNode.allChildrenSelected) {
parentCheckedNode.isSelected = true;
}
if (parentCheckedNode.isSelected) {
checkedPaths.add(parentPath);
} else {
checkedPaths.remove(parentPath);
}
// Go to upper predecessor
updatePredecessorsWithCheckMode(parentPath, check);
}https://stackoverflow.com/questions/31532506
复制相似问题