• Post Reply Bookmark Topic Watch Topic
  • New Topic

show plus sign initially for nodes without children  RSS feed

 
pavan in
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a dynamically generatd jtree. The jtree nodes are obtained from the database. Initially, only one level is shown. Rest is obtained from database using TreeWillExpandListener. Since, initially the nodes does not have children, it'll not show '+' sign before the node and hence I can not get the nodes children from database using TreeWillExpandListener. How can I get the '+' sign before the node initially even though it has no children.
I create a JTree but don't fully populate it due to performance reasons. I want my nodes to show the "+" sign if they have children "in the database", but the JTree won't know about them until the user tries to expand that node. I know I could add a dummy node under any nodes that have children, but is there a more elegent way?
 
Bill Liteplo
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is common to want to avoid loading client-side objects for everything in a hierarchy until the user requests them explicitly. This is called lazy loading.
First question: are your parent nodes that do have children declared as leaves or not? I.e. each of them is a Node, and at some point returns either true or false for isLeaf().
DefaultMutableTreeNode will return true for isLeaf if getAllowsChildren returns false. Make sure your parent nodes return true for getAllowsChildren (and false for isLeaf).
Use the above methods to tell the TreeUI to give your parent nodes an expansion widget (+ box). Do a special trick in getChildAt and getChildCount to delay the loading. Initially say it has no children. Then, when user clicks on expansion handle, begin loading children.
Hope this makes sense.
Bill
 
Jawad Kakar
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a newly created node will have a child, add a fake child (may be a Boolean Object), this will signify that the parent node has not been expanded.
Now when a node is expanded, its list of childern is examined. Three situations are possible.
1: No Childern. This node is a leaf.
2: One "flag" child is present. That node has childern which have not been added yet, so we create these childern and add new nodes to the parent node.
3: One or more "non-flag" childern are present. this node has already been processed, so exapnd it as usual.
Jawad
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!