File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTree collapsed after refresh Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree collapsed after refresh" Watch "JTree collapsed after refresh" New topic
Author

JTree collapsed after refresh

Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75
Hello All,

I have some parent nodes in tree connected to root node. After refresh there are chances of adding or removing child nodes of parent nodes. For refresh I am deleting the parent node and then adding it with new refreshed data(child nodes).
Before deleting node I am storing state of node whether it is expanded or not, and restoring this state after refresh. So after refresh I always get updated nodes with correct state expand or collapse. But after refresh when I click on any child node whole tree get collapsed, do not know why it is happening?

I am using DefaultTreeModel, DefaultMutableTreeNode and JTree
Code to remove node is -

Code to add node is -


Please anybody can give me direction about what might be going wrong, or what I have missed?
I have very big code so have shown only some part of it, I sincerely apologize for inconvenience you might face while helping me.

Regards,
Sham P.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1939
    
  28
It's hard to tell with so little code to go on but I'm not convinced you are expanding the nodes properly. fireXXX is normally a mechanism to notify listeners something has happened and not a way of doing something (ie expanding the nodes) as your comment suggests.

BTW Why aren't you using the JTree's expandPath(..) method to expand the node?
Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75
Hello Tony, thanks for your reply.

I have implemented TreeExpansionListener and in treeExpand and treeCollapsed method I am expanding and collapsing tree nodes. So when nodes are collapsed there are no child nodes. Previously keeping child nodes if they are hidden under parent and if some updates come my application was hanging. So now I am doing it in TreeExpansionListener.

Today I have added some code after expanding node by calling fireTreeExpanded() method, and it solved the problem. I am wondering how it is solved...

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1939
    
  28
I'm not sure but it may be that by handling this yourself from within TreeExpansionListener you are not firing some event(s) that need to be fired to keep everything in sync. I guess by then calling collapseRow/expandRow these events are being fired and so the JTree and it's model are then back in sync.
Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75
Actually I am calling nodeStructureChanged() method within TreeExpansionListener, I think it should handle firing of events.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1939
    
  28
As I said earlier I'm not sure what the reason is but the collapsed and expanded events are generated by the JTree object and not it's model. nodeStructureChanged() is a method in the model and I don't know if calling this method will result in a firing of tree expansion event. You could always look at the source code or add a few listeners to your code to print out what is happening.
Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75
While collapsing/expanding rows using expandRow() or collapseRow(), which events does get fire? Is there other way to fire them?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1939
    
  28
As I said before you'll have to look at the source code, but at the very least the events for the TreeExpansionListener will be fired and probably the events for the TreeWillExpandListener will also be fired, other events may also be fired. It's just not possible to say without looking at the code.

Yes you can fire them yourself, JTree has public fireXXX methods for these events.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTree collapsed after refresh
 
Similar Threads
JTree, adding and removing nodes contained in a master tree
Expand Lazy Loaded JTree
JTree
Searching a JTree
Lazy fetching JTree