• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Lazy fetching JTree

 
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,

I am trying to make a JTree to lazy fetch it's nodes from the database. How can I do that? If i use treewillexpandlistener and only fetch children when the user click a parent node, the event won't be triggered unless the X parent has children (so I must fetch all the children from the database at init time).

Thanks in advance
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the return value of isLeaf determines whether or not to show the expand icon. There are now two options:
- always return false; but then even leafs will appear as non-leafs
- fetch the children when needed, but only when needed

I've written a TreeModel implementation that uses the second technique. It loads the data anytime one of the methods that involve the children is called and the data wasn't loaded before. This does not mean that the entire tree is prefetched; only the direct children of the visible nodes are prefetched. So if initially you only show the root, then only the root's children are prefetched. If you open the root, the children of the root's children will be prefetched, etc.

Now, if you can store the number of children in the elements, then you don't need this. Simply let isLeaf return whether this number is 0 or not. Then load the data when the node is expanded.
 
Andrei Antonescu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.
That helped & worked
 
Rob Spoor
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic