Results 1 to 5 of 5
  1. #1
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default Get child of DefaultMutableTreeNode

    Hello.

    I have a DefaultMutableTreeNode.

    This DMTN has several children.

    I want to do something like this:

    Java Code:
    for(int i = 0; i< dmt.getChildCount(); i++){
    
    [INDENT]System.out.println(dmt.getChildAt(i).getUserObject().toString());[/INDENT]
    
    }
    Simple enough. However, apparently the method getChildAt(int) returns an object of type TreeNode rather than DefaultMutableTreeNode.

    TreeNodes don't have a getUserObject() method.


    ALSO, I am not allowed to cast a TreeNode to a DefaultMutableTreeNode.

    Seriously java? Why can't I get the child of a DefaultMutableTreeNode as a DefaultMutableTreeNode.

    Please let me know if there is a method of doing what I want to do (iterate through the children of my dmtn and do something with their userObject).

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,345
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by PrinceSendai View Post
    ALSO, I am not allowed to cast a TreeNode to a DefaultMutableTreeNode.
    The getChildAt( ... ) method is defined in the interface TreeNode and that interface knows nothing about DefaultMutableTreeNodes; but you do: if all your nodes in the tree are DefaultMutableTreeNodes you can cast all you want.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default

    Okay, I think my tree is not made of DefaultMutableTreeNodes.

    I generated my tree from vectors, which ultimately use a custom-made object as values.

    I wanted to work with this custom made object that I generated the tree from.

    How do I work with the objects of the tree, given the fact that I generated it from vectors?

    OR is there no way to retrieve the values from the tree once I have generated it from vectors?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,345
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by PrinceSendai View Post
    Okay, I think my tree is not made of DefaultMutableTreeNodes.

    I generated my tree from vectors, which ultimately use a custom-made object as values.

    I wanted to work with this custom made object that I generated the tree from.

    How do I work with the objects of the tree, given the fact that I generated it from vectors?

    OR is there no way to retrieve the values from the tree once I have generated it from vectors?
    I guess all your nodes are DefaultMutableTreeNodes; show us your code where the cast fails; I bet you have your parentheses wrong ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default

    Thanks. You were right. I think it was a parentheses issue. Now the cast works.

Similar Threads

  1. DefaultMutableTreeNode
    By vendetta in forum New To Java
    Replies: 0
    Last Post: 03-02-2010, 04:15 PM
  2. How to add different menuitems to child node
    By sudhakar.cheru in forum AWT / Swing
    Replies: 2
    Last Post: 09-16-2009, 06:32 AM
  3. How to get value of specific child node
    By sito42 in forum New To Java
    Replies: 1
    Last Post: 07-13-2009, 12:00 PM
  4. Cannot add or update a child row....
    By Pierced1 in forum JDBC
    Replies: 2
    Last Post: 06-29-2009, 02:32 AM
  5. Child Labor
    By freddieMaize in forum Forum Lobby
    Replies: 1
    Last Post: 11-16-2008, 08:59 PM

Tags for this Thread

Posting Permissions

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