aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Dont collapse JTree after refresh? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Dont collapse JTree after refresh?" Watch "Dont collapse JTree after refresh?" New topic
Author

Dont collapse JTree after refresh?

Max Power
Ranch Hand

Joined: May 10, 2004
Posts: 47
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

Joined: Sep 07, 2002
Posts: 14
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

Joined: May 10, 2004
Posts: 47
hi, thanks :-). i am implementing treemodel, can i use your first option anyway?

thanks
slade cox
Greenhorn

Joined: Sep 07, 2002
Posts: 14
Yes you can. On the page mentioned above see the FileExplorer example and the Dynamically Modifying a Tree section.
Max Power
Ranch Hand

Joined: May 10, 2004
Posts: 47
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

Joined: Sep 07, 2002
Posts: 14
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

Joined: May 10, 2004
Posts: 47
hi, yeah thats it...tHANKS
slade cox
Greenhorn

Joined: Sep 07, 2002
Posts: 14
Glad to help. Out of curiosity, how smoothly does the JTree work?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Dont collapse JTree after refresh?