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 Populating JTree on a 'As Needed' basis Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Populating JTree on a Watch "Populating JTree on a New topic
Author

Populating JTree on a 'As Needed' basis

zulfiqar raza
Ranch Hand

Joined: Oct 18, 2000
Posts: 81
Hello folks, I have a JTree question:
I have 3 main tables from which I need to populate my JTree. I do not want to populate all of JTree at the time when the JTree instance is created, as I assume the database calls would slow the instantiation. Also I would like to have finer control on populating the nodes, I will have to dynamically modify the tree later on.
Presently I am sub classing the DefaultMutableTreeNode to create 3 nodes specific to the underlying tables. And using these sub classes in creating the JTree.
What should be the route to go so that the JTree only poplulates its nodes when a user opens a particular node. Also the node should cache the children so that subsequent clicks on the same node does not make additional calls to the SQL database , unless it needs to refresh itself?
Should I be implementing this in a subclass of DefaultTreeModel or in the subclass of the DefaultMutableTreeNode??
Also the dynamic modification of the TreeNode should that be handled in the DefaultTreeModel or the DefaultMutableTreeNode??
thank you,
zulfiqar
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
Zulfigar,
I've done something like what you're talking about using a TreeWillExpandListener. Use JTree's addTreeWillExpandListener() method to register your listener, and it will be called before a tree node is expanded. When that occurs, you can dyanamically create the node's children, allowing you to populate your tree on an as-needed basis.
------------------
Brett Spell
Author, Professional Java Programming
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Populating JTree on a 'As Needed' basis
 
Similar Threads
Select a node in Jtree
Searching a JTree
JTree
JTree help
JTree not updating