This code worked for me only partially. Once I tried selecting deeper nested nodes, I ran into problems. Since my production code is very cluttered I built an example and reproduced my exact problem in it.
The important method is 'manualSelect(String)'.
It streps through the tree by comparing user objects and finds the target node. Then it calls 'navigateToNode()' which uses the solution discussed in the links above.
This method behaves incosistently:
If you call it with an empty array it will correctly select the root node. The righthand pane shows the text of the selected node. = correct
If you call it with target node 1 it will correctly select node 1. The righthand pane shows the text of the selected node. = correct
If you call it with target node 1.1 it will select node 1.1 but for some reason it is not displayed as marked (no background). The righthand pane shows the text of the selected node. = only partly correct
If you call it with target node A it will run into an exception. Debugging reveals that the selection occurs correclty at first. You can also observe the righthand pane showing that 'A' was selected. But afterwards another TreeselectionEvent is occuring that has a NewLeadSelectionPath of null. I do not know where this second SelectionEvent is triggered from.
Am I doing something wrong? Has anyone ideas on solving this problem?
This code is run with Java 1.6.0_39 which is also what I will have to use in production.
posted 5 years ago
A collegue finallly helped solve this.
The key is to temporarily remove the TreeExpansionListener when stepping through the JTree manuallly and reAdding it afterwards.