• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lazy fetching JTree

 
Andrei Antonescu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you very much.
That helped & worked
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic