Achilles
Achilles

Reputation: 1065

Select all checkbox nodes in a jtree

I have a checkbox node tree build based on a named vector . And i have a button called select all . When i click the select all button , i need all the nodes on the chekbox node tree to be selected .

The code i have used is

 for (CheckBoxNode rowNode: CheckBoxNodeTree. checkBoxCoulmn) 
{
   if(rowNode instanceof CheckBoxNode)
   rowNode.setSelected((true));
}

Here checkBoxColumn is a arraylist which contains all the nodes of the tree as (Node , isSelected) .

But when i do this , nothing happens to the tree.

Upvotes: 3

Views: 1720

Answers (1)

Dodd10x
Dodd10x

Reputation: 3349

I've done it by casting the tree node to a default mutable tree node and get an enumeration of the children. Then you can iterate through them and setSelected(true). Your way could run into problems with threading or concurrent modifications if the user repeatedly clicks.

Enumeration<TreeNode> children = ((DefaultMutableTreeNode) node).breadthFirstEnumeration();
 while (children.hasMoreElements()) {
     TreeNode child = children.nextElement();
     Object currentNode = ((DefaultMutableTreeNode) child).getUserObject();
     //cast your currentNode to the check box and set selected or unselected.
 }

Also, are you doing this on the event dispath thread? If not that might be why you aren't seeing any updates to the screen.

Upvotes: 3

Related Questions