Results 1 to 4 of 4
  1. #1
    frednic is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Question Fill JTree with data

    I have to fill a JTree with data. The data are stored in a vecor (int, string). The int is the level number:

    e.g.
    1
    2
    3
    3
    4
    4
    3
    2
    1

    The element with the number 1 is the root element. I can add this to the tree like this:
    "root.add(new DefaultMutableTreeNode("Child 2"));"

    But how can I add the other elements without using a variable name for them? I can have up to 10 levels. I need the tree only to display the in information. No actionlistener needs to be added.
    Thanks in advance!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    how can I add the other elements without using a variable name for them?
    You can use DefaultMutalbeTreeNode#getChildAt(...) or TreeModel#getChild(...) and cast the returned TreeNode/Object to DefaultMutableTreeNode. Then add the child node to the returned object.

    I need the tree only to display the in information.
    You haven't said whether the displayed data will change over the runtime of the program. If not, I would write a single method to create, populate and return a DefaultTreeModel, then construct the JTree with that model.

    db

  3. #3
    frednic is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default JTree

    dear Darryl,

    The displayed data will not change over the runtime of the program.

    Thanks in advance

  4. #4
    frednic is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Sorry Darryl,

    The displayed data will not change over the runtime of the program.
    Thanks in advance for writing that method.

    frednic

Similar Threads

  1. Decimal Fill up
    By maya700 in forum New To Java
    Replies: 4
    Last Post: 07-28-2010, 07:15 PM
  2. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 12:59 PM
  3. How to Fill Arc in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:14 AM
  4. How to Fill a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:10 AM
  5. how to display data in Jtree
    By paty in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 01:28 AM

Posting Permissions

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