• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JTree collapsed after refresh

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I am calling nodeStructureChanged() method within TreeExpansionListener, I think it should handle firing of events.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While collapsing/expanding rows using expandRow() or collapseRow(), which events does get fire? Is there other way to fire them?
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
There's a city wid manhunt for this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic