Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-02-2009, 07:18 AM
Member
 
Join Date: Jun 2008
Location: Australia
Posts: 43
Rep Power: 0
javanewbie is on a distinguished road
Question How to dynamically add Nodes to JTree?
How to add nodes to a Jtree by getting the information into a specific file

For example, in JList:

Code:
DefaultListModel workgroups;
workgroups = new DefaultListModel();
List<String> workgroupsList = new ArrayList<String>();
workgroupsList = ParserUtils.getWorkgroupList(ParserUtils.getConfigPath() + "\\.workgroup.properties");
if (!workgroupsList.isEmpty()){
	for (String workgroup : workgroupsList){
		workgroups.addElement(workgroup.toString().trim());
	}
}

workgroupList = new JList(workgroups); //adds the list workgroups
The output of this will be a JList displaying the list of workgroups per line. The list could be found in a file name "workgroup.properties"


Question is, is it possible to adapt this same method in JTree. The information per line will serve as one node in the tree. For example, I have 3 workgroups in the list, there will also be 3 nodes to be found in the tree.


Any suggestions?

THanks.

Last edited by javanewbie; 07-02-2009 at 07:22 AM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-02-2009, 07:16 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,561
Rep Power: 4
hardwired is on a distinguished road
Default
Looks straight–forward enough
Code:
DefaultMutableTreeNode root = new DefaultMutableTreeNode(...
// every node is a DefaultMutableTreeNode
for each line
    DefaultMutableTreeNode nextChildNode = new DMTN(nextLine)
    root.add(nextChildNode)
    for each list(corresponding to this line) element 
        DefaultMutableTreeNode child = new DMTN(nextListItem)
        nextChildNode.add(child)
...
tree = new JTree(root)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-03-2009, 04:59 AM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 921
Rep Power: 2
Steve11235 is on a distinguished road
Default
All these components have a model that holds their data. The best thing to do is go through the tutorial and the api for each of the sub-components and figure out how they work together. In your case, your want to update the TreeModel
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-13-2009, 08:20 AM
Member
 
Join Date: Jun 2008
Location: Australia
Posts: 43
Rep Power: 0
javanewbie is on a distinguished road
Unhappy
Here's an update of what I'm doing with the codes


Code:
protected JPanel createServiceCallTreeWorkgroup2() {
    	  
    	
    	 List<String> WGList = new ArrayList<String>();
    		WGList.add("All Workgroups");
    		WGList = ParserUtils.getWorkgroupList(ParserUtils.getConfigPath() + "\\tracker.workgroup.properties");
    		if (!WGList.isEmpty()){
    			for (String workgroup : WGList){
    				WGList.add(workgroup);
    			}
    		}
    		
    		Object hierarchy[] = WGList.toArray();
    		
    		
    		
    		 DefaultMutableTreeNode root = processHierarchy(hierarchy);
    		    JTree WGTree = new JTree(root);
    		    WGTree.setPreferredSize(new Dimension(200, 220));

    	        
    			JPanel WGPane = new JPanel();
    	        WGPane.add(WGTree);
        return WGPane;
    }
and


Code:
private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
	    DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);
	    DefaultMutableTreeNode child;
	    for (int i = 1; i < hierarchy.length; i++) {
	      Object nodeSpecifier = hierarchy[i];
	      if (nodeSpecifier instanceof Object[]) // Ie node with children
	      {
	        child = processHierarchy((Object[]) nodeSpecifier);
	      } else {
	        child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
	      }
	      node.add(child);
	    }
	    return (node);
	  }
	}

Still doesn't work. Any suggestions?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Move JTree item to another JTree. Melki AWT / Swing 8 07-09-2009 12:59 PM
JcheckBoxes as JTree Nodes aneesahamedaa AWT / Swing 11 02-11-2009 01:11 AM
nodes Dr Gonzo New To Java 1 12-08-2008 05:22 PM
Nodes displayed in JTree Orange AWT / Swing 6 08-08-2008 06:07 AM
nodes in java ahsan New To Java 0 12-26-2007 04:09 PM


All times are GMT +2. The time now is 02:20 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org