• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with dynamic tree view

 
bob morkos
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this and I need to make it dynamic by addiing many tree nodes. I need some kind of loop for my (ob and oc).
<code>
DefaultMutableTreeNode Component =
new DefaultMutableTreeNode("Company");
//help here (I think of adding a loop here) (i,e: ob1, ob2, ...)
DefaultMutableTreeNode ob = new DefaultMutableTreeNode(st.nextToken());
DefaultMutableTreeNode obc = new DefaultMutableTreeNode(st.nextToken());
Component.add(ob);
ob.add(obc);
</code>
I don't know how to do it within a loop, could anyone show me within an example.
 
Shital Shisode
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attaching the code I am using in one on my projects. I am doing the same thing. Refer it and see if it helps.
===========================================================
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

DefaultMutableTreeNode child;
String lessonname[] = {"Child1","Child2 ","Child3"};
for(int i=0;i<lessonname.length;i++)>
{ child = new DefaultMutableTreeNode(lessonname[i]);
root.add(child);
}
tree = new JTree(root);
tree.addTreeSelectionListener(this); // event listener
setLayout(new BorderLayout());
add(new JScrollPane(tree), BorderLayout.CENTER);
===============================================================
This code creates a tree as follows :
Root
+ Child1
+ Child2
+ Child3

------------------
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!