Results 1 to 3 of 3
- 11-30-2010, 01:34 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
The insertNodeInto method doesn't work for my JTree
Hi!
Initially, I have a data set stored in the database and a BLANK JTree. This data set contains a tree structure, e.g.:
1. Documents
1.1 Correspondence
1.1.1 Incoming
1.1.2 Outgoing
1.2 Contracts
etc.
What I need, is to build the JTree based on the above-shown information. The problem is that insertNodeInto doesn't work properly in my code - only second-level nodes are added to the root node, while the path to the other nodes is not found. Where is the problem? Thanks!
This is output of System.out.printlnPHP Code:# public static void traverse( Object hierarchy[], Object hierarchyChildOf[], int k ) { for(int i = 0; i < hierarchy.length; i++) { if (hierarchy[k].equals(hierarchyChildOf[i])) { MutableTreeNode childNode = new DefaultMutableTreeNode(hierarchy[i]); try { TreePath path = tree1.getNextMatch(hierarchyChildOf[i].toString(), 0, Position.Bias.Forward); if (path != null) { MutableTreeNode parentNode = (DefaultMutableTreeNode) (MutableTreeNode) path.getLastPathComponent(); treeModel.insertNodeInto(childNode, parentNode, parentNode.getChildCount()); System.out.println("The child node " + hierarchy[i] + " is added to the parent node " + hierarchyChildOf[i]); } else { System.out.println("The path to the parent node " + hierarchyChildOf[i] + " cannot be found."); } } catch (Exception e) { e.printStackTrace(); } traverse(hierarchy, hierarchyChildOf, i); } } } public static void createTree() { ... tree1 = new JTree(); tree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); root = new DefaultMutableTreeNode(hierarchy[0]); treeModel = (DefaultTreeModel)tree1.getModel(); treeModel.setRoot(root); traverse( hierarchy, hierarchyChildOf, 0 ); ... }
PHP Code:The child node Contracts is added to the parent node Documents The child node Correspondence is added to the parent node Documents The path to the parent node Correspondence cannot be found. The path to the parent node Correspondence cannot be found. The child node Inner documents is added to the parent node Documents The child node Labour contracts is added to the parent node Documents
Last edited by LianaN; 11-30-2010 at 01:36 PM.
- 11-30-2010, 07:38 PM #2
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
I'll try to be more specific.
There are two arrays filled from DB:
* hierarchy[]: Documents, Correspondence, Incoming, Outgoing, Contracts
* hierarchyChildOf[]: null, Documents, Correspondence, Correspondence, Documents
For instance, hierarchy[1] (Correspondence) is a child of hierarchyChildOf[2] (Documents). Now I must populate JTree with these data. The task is just to add all nodes defined in hierarchy[] to corresponding parent nodes specified in hierarchyChildOf[].
Below you may find my comments on the code:
Any ideas how to implement it correctly?PHP Code:public static void traverse( Object hierarchy[], Object hierarchyChildOf[], int k ) { for(int i = 0; i < hierarchy.length; i++) { // We are searching children of hierarchy[k]. // If hierarchy[k] is equal to hierarchyChildOf[i], then it means that // hierarchy[i] is a child of hierarchy[k] if (hierarchy[k].equals(hierarchyChildOf[i])) { MutableTreeNode childNode = new DefaultMutableTreeNode(hierarchy[i]); try { // We are searching the node hierarchyChildOf[i] in the JTree->tree1 TreePath path = tree1.getNextMatch(hierarchyChildOf[i].toString(), 0, Position.Bias.Forward); if (path != null) { // If such node exists, then the childNode hierarchy[i] is added // to the parentNode hierarchyChildOf[i] MutableTreeNode parentNode = (DefaultMutableTreeNode)(MutableTreeNode) path.getLastPathComponent(); treeModel.insertNodeInto(childNode, parentNode, parentNode.getChildCount()); System.out.println("The child node " + hierarchy[i] + " is added to the parent node " + hierarchyChildOf[i]); } else { System.out.println("The path to the parent node " + hierarchyChildOf[i] + " cannot be found."); } } catch (Exception e) { e.printStackTrace(); } traverse(hierarchy, hierarchyChildOf, i); } } }
- 11-30-2010, 07:53 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
I've solved the problem in the following way:
PHP Code:Hashtable h = new Hashtable(); for (int i = 0; i < hierarchy.length; i++) { Hashtable child = new Hashtable(); for(int j = 0; j < hierarchy.length; j++) { if (hierarchy[i].equals(hierarchyChildOf[j])) { child.put(hierarchy[j],hierarchy[j]); } } if (!child.isEmpty()) h.put(hierarchy[i], child); } tree1 = new JTree(h);
Similar Threads
-
Strings made by scanner don't work with startsWith() method
By samza in forum New To JavaReplies: 13Last Post: 05-16-2010, 12:09 AM -
help~ delete method cant work
By reeveliew in forum New To JavaReplies: 4Last Post: 05-07-2010, 02:24 AM -
why doesnt my insertion sort method not work?
By Jeremy8 in forum New To JavaReplies: 7Last Post: 11-15-2009, 02:56 AM -
Move JTree item to another JTree.
By Melki in forum AWT / SwingReplies: 8Last Post: 07-09-2009, 11:59 AM -
how does the remove method work for sets and hashsets
By haridharna in forum Advanced JavaReplies: 4Last Post: 08-06-2007, 12:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks