0

I am developing an IntelliJ plugin will popup dialog to check values from CheckboxTree. for that I am following below Q&A:

Java Swing: Need a good quality developed JTree with checkboxes

But when I click on single child node the parent node is also getting selected. But I want to select parent node only when all its child are selected otherwise unselected.

Community
  • 1
  • 1
Karanbeer Kaur
  • 365
  • 4
  • 14

1 Answers1

1

Just add this code

Comment the code like below in updatePredecessorsWithCheckMode function and add the code after the for loop

// 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;
    }

Full function for reference

// 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);
}
Madhan
  • 5,750
  • 4
  • 28
  • 61
  • @ Madhan: Thanks a lot for your kind help... It is working absolutely fine.. :) – Karanbeer Kaur Jul 21 '15 at 10:01
  • @Mashan need more help. the String length of checkbox is large. But it is showing only 2 characters. I am change layout using below code chechBoxTree.setLayout(new BoxLayout(chechBoxTree, BoxLayout.PAGE_AXIS)); chechBoxTree.setSize(800,300); But it is not reflecting in UI. Kindly help. – Karanbeer Kaur Jul 21 '15 at 13:54
  • @user667505 I couldn't get you.If the text is too large is it showing only two characters followed by two dots or something like that.If so try `((DefaultTreeModel)tree.getModel()).reload();`.Else explain the problem more or post it as a different question – Madhan Jul 21 '15 at 15:05