• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic