2

I have a problem which was also described here.

I have the invertExpand method in MyTree class which works like this:

public void invertExpand(DefaultMutableTreeNode node) { TreePath path = new TreePath(node.getPath()); // no better way to get TreePath from TreeNode :( if (!isExpanded(path)) { expandPath(path); } else { collapsePath(path); } } 

But the problem is isExpanded() method uses the HashMap to store the expanded paths. It seems that isExpanded() never returns true for newly created TreePath. (But they are expanded really)

Does any way to fix that issue?

1
  • well, next time show a SSCCE that demonstrates the problem (instead of leaving that task to the answerer ;-) Commented Jul 18, 2013 at 10:45

1 Answer 1

5

Expand/collapse works on non-leaf nodes, so be sure that the node in question is not a leaf:

public void invertExpand(DefaultMutableTreeNode node) { if (node.isLeaf()) node = (DefaultMutableTreeNode) node.getParent(); TreePath path = new TreePath(node.getPath()); // no better way to get TreePath from TreeNode :( if (isExpanded(path)) { collapsePath(path); } else { expandPath(path); } } 

Edit (as per OP's comment)

The real reason for the misbehaviour was an incorrectly implemented hasCode in a custom node which confused the Map (where the expanded paths are stored).

Sign up to request clarification or add additional context in comments.

4 Comments

well, then your node implementation is incorrect - leafs can't be expanded, so get what you asked for (from the tree ;-) BTW: now it's really time for an sscce, without there's no way to answer your question ...
hmm @kleopatra please write in answer that isExpanded(), expandPath() and collapsePath() use the HashMap so I need to check the overrides of the equals() and hashCode().
hmmm, curious: why did you override equals, it's a bit unusual?
My node is quite complex, it's equality should not being determined with ALL of fields.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.