jQuery in Action, 3rd edition
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

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,
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 107
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
jQuery in Action, 3rd edition