• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding Nodes to a tree

 
Sherfuddin Shah Quadri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a query in JTree.

I have a root node rtNode added to the DefaultMutableTreeNode.

If i happened to add the subsequent nodes below the root node how will i do that??

I'm iterating through the collection and fetching the subsequent nodes..

private DefaultMutableTreeNode processHierarchy(Collection nodeCol) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(getRootNode());
DefaultMutableTreeNode child;


for (Iterator iter = nodeCol.iterator(); iter.hasNext() {
Emp empElement = (Emp) iter.next();

if (empElement instanceof Collection){

//The Control is not getting here..
//Please correct the code also..

child = processHierarchy((Collection) empElement);

}else{

child = new DefaultMutableTreeNode(empElement);

}
node.add(child);
}

return(node);
}


Pls check the code too..

Thanks in Advance
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if this makes sense for a tree with one boss, two managers and four workers.

I took a slightly tricky shortcut by creating a node, assigning it to a variable and adding it to a parent node all in one line. Lemme know if you find that unreadable and we can split it out.

But the point is you can add child nodes to a parent node to build the tree. But you'd probably structure the code to match the data better:

Zat help?
 
Sherfuddin Shah Quadri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u james..

But can u pls let me know how I can add a Collection to a Tree??(With an Example)

I think that'll be more useful to me..

I tried it.. But there is some repetation of Nodes..

pls reply me at the earliest..
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my second example I showed looping through an array of managers or workers to add nodes. You could do the same with any collection - just get an iterator and add a node for each nextElement(). If you have the boss, managers and workers all in one collection you'll have some scheme for indicating the reporting structure. I can't guess what that might be. Can you describe the collection(s) you're working with in more detail?
 
Sherfuddin Shah Quadri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I have got a collection, which contains all the Employee Objects..
The employee object denotes an employee in the Organisation.. For instance, The CEO becomes the Root, then there are a set of managers below the employees and each manager has a set of supervisors and each supervisors have workers below them.

Example. CEO is the root, Manager A has 3 Supervisors. Each Supervisors hav 4 Workers under them (The values are not staic it may vary). A worker may have more than 2 Supervisors..

Now I hav to hav a recursive function to add these employees to the tree..

There is a class Employees which contains a function getEmployees() that return a collection of Employee object. I first have to add the employees and i hav to check if these employees hav Sub Employees.. I can get that using the function getSubEmployees(Employee);

That is what I hav tried in the Code that I hav posted earlier..

Pls let me know how to add these objcets in the JTree using recursion

Thanks











managers hav Manager A 3 supervisors under him.. Each supervisors hav got
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!