Results 1 to 7 of 7
Thread: Building a Jtree from Array
- 03-26-2011, 11:45 AM #1
Member
- Join Date
- May 2010
- Posts
- 15
- Rep Power
- 0
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.
ThanksLast edited by rgeurts; 03-26-2011 at 11:50 AM.
- 03-26-2011, 12:45 PM #2
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
- 03-26-2011, 05:27 PM #3
Member
- Join Date
- May 2010
- Posts
- 15
- Rep Power
- 0
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); } }
- 03-26-2011, 07:18 PM #4
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.
dbJava 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); } }Last edited by DarrylBurke; 03-27-2011 at 09:22 AM.
- 03-26-2011, 07:50 PM #5
Member
- Join Date
- May 2010
- Posts
- 15
- Rep Power
- 0
Thanks!
Thanks for posting a workable exmaple Darryl,
This will give me something think about tonight
Regards
- 03-26-2011, 08:55 PM #6
Member
- Join Date
- May 2010
- Posts
- 15
- Rep Power
- 0
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
- 03-27-2011, 09:23 AM #7
Similar Threads
-
JTree - Save and load from an array?
By grahamb314 in forum AWT / SwingReplies: 10Last Post: 04-14-2011, 09:52 AM -
Building GUI
By alacn in forum New To JavaReplies: 7Last Post: 06-17-2010, 04:32 AM -
Building a HSM Simulator
By stunnaz101 in forum Advanced JavaReplies: 0Last Post: 03-23-2010, 10:01 AM -
Move JTree item to another JTree.
By Melki in forum AWT / SwingReplies: 8Last Post: 07-09-2009, 11:59 AM -
building a house
By dc2acgsr99 in forum Java AppletsReplies: 4Last Post: 03-07-2008, 11:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks