File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Lazy fetching JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Lazy fetching JTree" Watch "Lazy fetching JTree" New topic
Author

Lazy fetching JTree

Andrei Antonescu
Ranch Hand

Joined: Jul 08, 2010
Posts: 75
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

Joined: Oct 27, 2005
Posts: 19783
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Andrei Antonescu
Ranch Hand

Joined: Jul 08, 2010
Posts: 75
Thank you very much.
That helped & worked
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lazy fetching JTree