Results 1 to 7 of 7
  1. #1
    rgeurts is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default Building a Jtree from Array

    Hi All,

    I am fairly new to java and I am struggling with the JTree object.

    Basically, I want to populate the JTree with the following data:

    String[][] userGroups = {{“group 1, user1}, {group 1, user2}, {group 1, user3}, {group 2, user 4}, {group 2, user5,}, {group 3, user6}}

    I would like the JTree to look like this:

    Root
    Group 1
    - User1
    - User2
    - User3

    Group 2
    - User4
    - User5

    Group 3
    - User6


    Can someone please point me in the right direction on how to achieve this?

    p.s.
    I spent the last 3 days on trying to get it to work but without any luck.

    Thanks
    Last edited by rgeurts; 03-26-2011 at 11:50 AM.

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

    Default

    Let's see your best efforts, in the form of an SSCCE, in code tags, along with a description of any remaining problems and/or a specific question.

    db

  3. #3
    rgeurts is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default

    Thanks for getting back to me so quickly Darryl.

    I've successfully managed to add nodes for each of the groups.
    But I am struglling to add the users to the group (see code in red)


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JTree.*;
    import java.awt.BorderLayout;
    import java.util.*;
    
    
    public class JTreeObject extends JFrame{
     
      String[][] userGroups = {{"group 1", "user1"}, {"group 1", "user2"}, {"group 1", "user3"}, {"group 2", "user 4"}, {"group 2", "user5"}, {"group 3", "user6"}};
    
    
      public JTreeObject(){
        super("JTreeobject frame");
        setSize(300, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    
      public void init(){
        [COLOR="Red"]Hashtable hash = new Hashtable();
        for (int i = 0; i < userGroups.length; i++){
        hash.put(userGroups[i][0], userGroups[i][1]);[/COLOR]
        }
        JTree tree = new JTree(hash);
        getContentPane().add(tree, BorderLayout.CENTER);
      }
    
      public static void main(String args[]){
        JTreeObject object = new JTreeObject();
        object.init();
        object.setVisible(true);
      }
    }

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

    Default

    Hashtable implements Map, and you can't have multiple values associated with a key. You seem to have misunderstood the JTree constructor that takes a Hashtable (granted, the documentation is terrible!). Each key needs to have a value associated that represents its children. That value can be an array of leaf nodes, or another Hashtable again with keys and values.
    Java Code:
    import java.awt.BorderLayout;
    import java.util.Enumeration;
    import java.util.Hashtable;
    import javax.swing.*;
    
    public class JTreeObject extends JFrame {
    
      String[] groups = {"group 1", "group 2", "group 3"};
      String[][] users = {{"user1", "user2"}, {"user3", "user4"}, {"user5", "user6"}};
    
      public JTreeObject() {
        super("JTreeobject frame");
        setSize(300, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    
      public void init() {
        Hashtable hash = new Hashtable();
        for (int i = 0; i < groups.length; i++) {
          hash.put(groups[i], users[i]);
        }
    
        JTree tree = new JTree(hash);
        getContentPane().add(tree, BorderLayout.CENTER);
      }
    
      public static void main(String args[]) {
        JTreeObject object = new JTreeObject();
        object.init();
        object.setVisible(true);
      }
    }
    db
    Last edited by DarrylBurke; 03-27-2011 at 09:22 AM.

  5. #5
    rgeurts is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default Thanks!

    Thanks for posting a workable exmaple Darryl,

    This will give me something think about tonight

    Regards

  6. #6
    rgeurts is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default

    Hi Darryl,

    Would you mind explaining why you put the enumerator in the code?
    I can't figure out what the purpose is?

    Thanks again

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

    Default

    The enumerator was related to another question I was working on, just plugged it in to see the debug output. It's not required for your problem, and I've removed it from the code.

    Sorry about that.

    db

Similar Threads

  1. JTree - Save and load from an array?
    By grahamb314 in forum AWT / Swing
    Replies: 10
    Last Post: 04-14-2011, 09:52 AM
  2. Building GUI
    By alacn in forum New To Java
    Replies: 7
    Last Post: 06-17-2010, 04:32 AM
  3. Building a HSM Simulator
    By stunnaz101 in forum Advanced Java
    Replies: 0
    Last Post: 03-23-2010, 10:01 AM
  4. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM
  5. building a house
    By dc2acgsr99 in forum Java Applets
    Replies: 4
    Last Post: 03-07-2008, 11:18 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
  •