Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > pls help!!unable to add new node under existing parent

Reply
Thread Tools

pls help!!unable to add new node under existing parent

 
 
java4suj java4suj is offline
Junior Member
Join Date: Nov 2006
Posts: 1
 
      11-16-2006
Hi ,

I am trying to build a jtree with input from a arraylist.i need to form
a tree as below

OMCR
|--Shara
| |--AN-01
| |---router-1
| |--AN-02
| |---router-2
|---Merc

The nodes in the array list are in child, parent format.

EX:

Child : Parent
Shara : OMCR
AN-01 : Shara
router-1: AN-01
AN-02:Shara
router-2:AN-02

But i am not bale to add the nodes as required.Below is my code.Can
someone pls help me.Im just a week old in java and have not much idea.

DefaultMutableTreeNode root = new DefaultMutableTreeNode("OMCR");
---
---
---
.....................DefaultTreeModel treeModel = new
DefaultTreeModel( root );
tree = new JTree( treeModel );
........................
public void buildTree( DefaultMutableTreeNode root) {

DefaultMutableTreeNode child, parent,pseudoparent ;
HashMap name2TreeNode = new HashMap();
ArrayList matrix1 = new md().md_arraylist();
String lastchild = null;
TreePath path = new TreePath (root.getPath());
String childName;String parentName;

for(int i = 0; i < matrix1.size();i++){

pseudoparent = new DefaultMutableTreeNode(lastchild);
childName = (String)((ArrayList)matrix1.get(i)).get(0) ;
parentName = (String)((ArrayList)matrix1.get(i)).get(1);
child = new DefaultMutableTreeNode(childName);
parent = new DefaultMutableTreeNode(parentName);
System.out.println(" i : " + i +" from DB parent " + parent + "
child " + child );

if (parentName.equals("OMCR") ){
System.out.println("if 1" + " child is : " + child);
if (root.getIndex(child) == -1){
root.add(child);
lastchild = childName;
pseudoparent = child;
path = new TreePath (child.getPath());

}
}else{
if (!(lastchild.equals(null))) {
if (! (lastchild.equals(parentName))) {
path = new TreePath (parent.getPath());
parent =(DefaultMutableTreeNode)
path.getLastPathComponent();
System.out.println("if 2 last child " + lastchild + "
parent" + parent + " child " + child);
//root.add(parent);
parent.add(child);
lastchild =childName;

}else{
//lastchild equals parent

path = new TreePath (pseudoparent.getPath());


parent =(DefaultMutableTreeNode)
path.getLastPathComponent();


System.out.println("else part" + "last child " + lastchild +
" parent" + parent + " child " + child);
parent.add(child);
lastchild = childName;
}
}
}


} //end of for
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Extracting the first child node of a parent node ofuuzo1@yahoo.no XML 2 03-17-2008 12:31 PM
Null parent node on custom tree node after populate on demand John Bankhead ASP .Net Web Controls 0 12-04-2006 06:29 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM



Advertisments