Reputation: 187
I have a checkbox group Listed in a tree structure.The tree is connected with a vector, which stores the state of checkboxes in the tree. I have buttons to select all checkboxes, and other buttons to select the corresponding checkbox. From the below diagram you can picturize UI.
for (CheckBoxNode Node : CheckBoxNodeTree.checkBoxRows) {
if(Node.isSelected()){
Node.setSelected(!Node.isSelected());
}
For Select All the code used is :
TreeModel model = TREE.getModel();
TreeNode rootofTree = (TreeNode) model.getRoot();
Enumeration<TreeNode> enumeratorForTree = ((DefaultMutableTreeNode)rootofTree).breadthFirstEnumeration();
while (enumeratorForTree.hasMoreElements()) {
TreeNode child = enumeratorForTree.nextElement();
Object currentNode = ((DefaultMutableTreeNode) child).getUserObject();
if(currentNode instanceof CheckBoxNode) {
((CheckBoxNode) currentNode).setSelected(true);
}
}
for (CheckBoxNode Node: CheckBoxNodeTree.checkBoxRows)
{
Node.setSelected(true);
}
The issue i am facing now is that on clicking the respective buttons the checkbox state changes, but after clicking "Select All" button i am able to see that the nodes get checked ,but after this , if i try to select the induvidual nodes using the corresponding button , i cannot see the result on the tree . Can anyone Help me with your suggestions. Thanks to the replier in advance.
Upvotes: 0
Views: 1659
Reputation: 51525
Looks like a notification issue - you are changing node state without the model knowing of it. Assuming your model is a DefaultTreeModel, invoke a model.nodeChanged after changing the selection:
currentNode.setSelected(newState);
model.nodeChanged(currentNode);
Upvotes: 2
Reputation: 3349
Where is the code for your buttons used to select individual nodes? Are you trying to make a button that toggles but yours only checks the box right now? Maybe try this:
buttonPushed() {
//get your node for this button
node.setSelected(!node.isSelected());
}
Upvotes: 0