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

Discussion in 'Java' started by java4suj, Nov 16, 2006.

  1. java4suj

    java4suj

    Joined:
    Nov 16, 2006
    Messages:
    1
    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
    java4suj, Nov 16, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. viveknatani@gmail.com
    Replies:
    0
    Views:
    1,396
    viveknatani@gmail.com
    Feb 13, 2006
  2. Tjerk Wolterink
    Replies:
    2
    Views:
    1,399
    Dimitre Novatchev
    Aug 24, 2006
  3. ofuuzo1@yahoo.no
    Replies:
    2
    Views:
    812
    Martin Honnen
    Mar 17, 2008
  4. remlostime
    Replies:
    4
    Views:
    346
    Thomas J. Gritzan
    Sep 5, 2008
  5. John Bankhead

    Null parent node on custom tree node after populate on demand

    John Bankhead, Dec 4, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    268
    John Bankhead
    Dec 4, 2006
Loading...

Share This Page