I am working with JTree.
I would like to know what is best the way to know which nodes are expanded in a JTree so as to save its state (i.e. save all expanded paths). So that if I call model.reload()
the Jtree would not stay collapsed, but I will be able to restore its original state to the user, i.e., all expanded nodes will be expanded.
I'm new to Java and this drove me nuts as well. But I figured it out...I think. Below works fine in my app, but I think it does have some risk of not working as expected in some unusual circumstances.
Santhosh Kumar is one of my go-to guys for Swing Hacks.
Answer: http://www.javalobby.org/java/forums/t19857.html
You need to store the TreePaths that were expanded and expand them again after reloading the TreeModel. All TreePaths that have a descendant are considered to be expanded. P.S. if you deleted paths, check after reloading if the path is still available.