Results 1 to 4 of 4
  1. #1
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    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:

    Java 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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Looks straight–forward enough
    Java 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)

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    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

  4. #4
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    Unhappy

    Here's an update of what I'm doing with the codes


    Java 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


    Java 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?

Similar Threads

  1. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 12:59 PM
  2. JcheckBoxes as JTree Nodes
    By aneesahamedaa in forum AWT / Swing
    Replies: 11
    Last Post: 02-11-2009, 01:11 AM
  3. nodes
    By Dr Gonzo in forum New To Java
    Replies: 1
    Last Post: 12-08-2008, 05:22 PM
  4. Nodes displayed in JTree
    By Orange in forum AWT / Swing
    Replies: 6
    Last Post: 08-08-2008, 06:07 AM
  5. nodes in java
    By ahsan in forum New To Java
    Replies: 0
    Last Post: 12-26-2007, 04:09 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
  •