Results 1 to 8 of 8

Thread: swing trees

  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default swing trees

    I am having a hard time understanding the next code concerning the tree class.
    (read the code fragments like this a) 1, 2, 4 b) 1, 3, 4
    The selectedNode appears at the end of the searched path of the first two lines.
    At the next code fragment one creates a newNode which is inserted at the model.insertNodeInto.
    Question why is the third pram (int index): selectedNode.getChildCount()?

    There is an additional code (fragment 3) which looks up the parent node.
    Here the model.insertNodeInto inserts the created newNode, at the parent with the
    correct +1 index.
    Question what is the difference between the two codes?



    Java Code:
      DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) 
                   tree.getLastSelectedPathComponent();
                if (selectedNode == null) return;
    Java Code:
      DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");
                model.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());
    Java Code:
       DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
                if (parent == null) return;
                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");
                int selectedIndex = parent.getIndex(selectedNode);
                model.insertNodeInto(newNode, parent, selectedIndex + 1);

    Java Code:
    TreeNode[] nodes = model.getPathToRoot(newNode);
                TreePath path = new TreePath(nodes);
                tree.scrollPathToVisible(path);

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: swing trees

    Quote Originally Posted by willemjav View Post
    Question why is the third pram (int index): selectedNode.getChildCount()?
    The node already has getChildCount(0 children, with indexes ranging from 0 to getChildCount() -1. Does that answer your question?

    Quote Originally Posted by willemjav View Post
    Question what is the difference between the two codes?
    One adds a node as a child of the currently selected node. The other adds a node as a sibling of the currently selected node -- as a child of its parent.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: swing trees

    I noticed that the index/ childcount shows the children connected with a node, so it is relative to node, here the selected node.
    It is a little confusing: leafs, branches, parents, children and siblings....
    Siblings are nodes that share the same parent node, right?
    child is 1 2 4
    sibling is 1 3 4

    It seems here is no difference;
    "One adds a node as a child of the currently selected node. The other adds a node as a sibling of the currently selected node -- as a child of its parent."
    A child and a sibling both have a parent (always)?

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: swing trees

    db would you elaborate some more why one is a child and the other a sibling according the code difference?
    thanks!
    Last edited by willemjav; 10-05-2013 at 09:13 PM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: swing trees

    What are the arguments to insertNodeInto?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: swing trees

    Yes db, of course I noticed that both have the new created "newNode" in common. That node has to be inserted, right! But where? Parent is one move up, selectedIndex + 1 (from the parent) is one move down, and "selectedNode.getChildCount()" is (probably) relative from (if there are) other children....
    You see db these things should be explained very well and the book I was reading does not shine a clear light on all this!
    So I am now reading Core JFC java Foundation Class of Kim Topley, and you know what? I start to understand it!

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: swing trees

    Give it time. Oh, and getChildCount() returns exactly what the method name says.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

Similar Threads

  1. Help with binary trees please
    By sim18 in forum New To Java
    Replies: 3
    Last Post: 11-27-2012, 07:34 PM
  2. Visuallizing Trees
    By mike_ledis in forum Advanced Java
    Replies: 2
    Last Post: 01-17-2012, 09:24 PM
  3. Dynamic trees using Swing
    By Robertito in forum New To Java
    Replies: 7
    Last Post: 04-06-2011, 02:11 AM
  4. Splay Trees
    By Growler in forum New To Java
    Replies: 0
    Last Post: 11-02-2010, 02:10 PM
  5. Need help with Trees...(8-puzzle)
    By ventrue in forum New To Java
    Replies: 2
    Last Post: 03-23-2009, 11:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •