Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

TreeWillExpandListener does not handle expansion of node with no children...

 
Volodymyr Lysenko
Ranch Hand
Posts: 512
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I want to do lazy loading of tree nodes from database. I have to use TreeWillExpandListener for that.
The issue is that TreeWillExpandListener does not handle event when I try to expand node with no children.
In TreeWillExpandListener I want to go to database, get all data and then add children to node which to be expanded.

If I add dummy DefaultMutableTreeNode to node then TreeWillExpandListener works,
but I want to know if there is better way to fire TreeExpansionEvent when user tries to expand node with no children yet.

Thank you!
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "expand" facility is available only if the node has children. You will need to manipulate your code to accomodate this. Then when the user expands the node check if it was expanded before. If yes, show the cached nodes. Else go to the DB and generate your child nodes at runtime
 
Volodymyr Lysenko
Ranch Hand
Posts: 512
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply!

Yes, at first I check if node has zero children and if so I go to db and if nodeToExpand has children then nothing is performed.

The problem was that TreeExpansionEvent was not generated because node has no children.
I resolved it in this way.
I created MouseListener to listen for double click on JTree. Also I created KeyListener to listen on right arrow button and Enter.
When such events occur I can take source of event by e.getSource() which is JTree that generated event.
JTree has method to retrieve currently selected TreePath.
I use this TreePath to call method JTree.fireTreeWilExpandEvent(treePath) which fire TreeExpansionEvent handled by my TreeWillExpandListener
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!