• Post Reply Bookmark Topic Watch Topic
  • New Topic

Manually select a node in a JTree [problems with nested nodes]

 
Ole Hansen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have problesm with manually (through my java code) selecting items in a JTree. I did google the problem and found solutions here and here:
http://stackoverflow.com/questions/10242896/java-how-to-programmatically-select-and-expand-multiple-nodes-in-a-jtree
http://www.coderanch.com/t/543214/GUI/java/Select-node-Jtree

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.

DummyView.java


DummyTreeStarter.java


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.

Greetings,
Ole
 
Ole Hansen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A collegue finallly helped solve this.
The key is to temporarily remove the TreeExpansionListener when stepping through the JTree manuallly and reAdding it afterwards.
 
Campbell Ritchie
Marshal
Posts: 52587
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch and thank you for showing everybody the solution.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!