jQuery in Action, 3rd edition
The moose likes Swing / AWT / SWT and the fly likes Expand Lazy Loaded JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Expand Lazy Loaded JTree" Watch "Expand Lazy Loaded JTree" New topic

Expand Lazy Loaded JTree

Shams Pakistan

Joined: Jun 06, 2011
Posts: 6

I have a Lazyily loaded JTree but I am facing some problems.

I have a JTree which I load lazily using database query (each node). I have a button on the screen and when user clicks the button I perform some insert/update/delete in database and then add the model back in Jtree with only root and its immediate children using an SQL (this collapses the tree and selection and all expanded nodes collapse which is expected since it is a lazily loaded tree)

What I want to accomplish is before user clicks button to insert/update/delete, I read all the expanded state and save it in memory then when I re-initialize the Jtree I loop through those expanded children and call jtree.expandPath but it does not expand the nodes .

I even added a TreeWillExpandListener and in treeWillExpand event I added code to get the children of the node from database and I see that query does get executed to get the childreen but JTree still shows up as collapsed.

I really need your help. Please advise. Here is the code snippet.

Shams Pakistan

Joined: Jun 06, 2011
Posts: 6
I found the solution.

Need an equals method and hashCode in my NodeData class, problem was internally after setting the new Model when I was trying to expand the path the equals returned false as default implementation of equals does a "shallow compare" i.e. only compares references, I had to override the hashCode and equals method and did a "Deep compare" in equals method.

I agree. Here's the link: http://aspose.com/file-tools
subject: Expand Lazy Loaded JTree
It's not a secret anymore!