Results 1 to 4 of 4
- 07-02-2009, 06:18 AM #1
Member
- Join Date
- Jun 2008
- Location
- Australia
- Posts
- 43
- Rep Power
- 0
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:
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"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
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 06:22 AM.
- 07-02-2009, 06:16 PM #2
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)
- 07-03-2009, 03:59 AM #3
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
- 07-13-2009, 07:20 AM #4
Member
- Join Date
- Jun 2008
- Location
- Australia
- Posts
- 43
- Rep Power
- 0
Here's an update of what I'm doing with the codes
andJava 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; }
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
-
Move JTree item to another JTree.
By Melki in forum AWT / SwingReplies: 8Last Post: 07-09-2009, 11:59 AM -
JcheckBoxes as JTree Nodes
By aneesahamedaa in forum AWT / SwingReplies: 11Last Post: 02-11-2009, 12:11 AM -
nodes
By Dr Gonzo in forum New To JavaReplies: 1Last Post: 12-08-2008, 04:22 PM -
Nodes displayed in JTree
By Orange in forum AWT / SwingReplies: 6Last Post: 08-08-2008, 05:07 AM -
nodes in java
By ahsan in forum New To JavaReplies: 0Last Post: 12-26-2007, 03:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks