Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dont collapse JTree after refresh?

 
Max Power
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, is it somehow possible to save the expansion state of a jtree? the tree
collpases everytime i use the tree.setmodel method to refresh it....any ideas?

thank you :-)
 
slade cox
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First I would say that you are using TreeModel incorrectly if you are modifying the JTree by calling setModel. This should only be used for creating a new JTree. Instead you should modify your TreeModel and call the reload() method of DefaultTreeModel (which you extended, right?). For a full description and example see:

http://java.sun.com/products/jfc/tsc/articles/jtree/

If you absolutely insist on using the setModel method to update your tree then you could do something like this. Keep track of the expanded paths in a list. Register a TreeExpansionListener that does something like this:



Then after calling setModel iterate through the path list and call expandPath for each one. But, the first option is better.
 
Max Power
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, thanks :-). i am implementing treemodel, can i use your first option anyway?

thanks
 
slade cox
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can. On the page mentioned above see the FileExplorer example and the Dynamically Modifying a Tree section.
 
Max Power
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi again. i tried your second suggestion without success. :-( here is my example:



after 5 seconds im trying to expand the paths but i am getting a classcastexception!! can you see why?

THANKS A LOT
[ July 19, 2004: Message edited by: Andreas Golchert ]
 
slade cox
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the full exception message? ie. which line is causing the ClassCastException, which object is incorrectly cast?

Also, when you iterate through myPathList and expand the paths you could do something simpler like

for(int li=0;li<myPathList.size();li++){
t.expandPath((TreePath)myPathList.get(li));
}
 
Max Power
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, yeah thats it...tHANKS
 
slade cox
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to help. Out of curiosity, how smoothly does the JTree work?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic