Hi!
There is JTree component that I want to fill from the database. New nodes can be dynamically added to this tree. I need a procedure that will recursively traverse a tree. My problem is that all nodes are added to the root node, and sub-trees are not created. I've included System.out.println and it shows correct results. So, I just need to know how to add a new node to the correct parent node. Below is my code:
public static void createTree() {
Vector ofTitles = new Vector();
ofTitles = returnDataFromSelectQuery("select title from Folders");
Object hierarchy[] = ofTitles.toArray();
Vector ofChildOf = new Vector();
ofChildOf = returnDataFromSelectQuery("select childOf from Folders");
Object hierarchyChildOf[] = ofChildOf.toArray();
tree1 = new JTree();
tree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
node = new DefaultMutableTreeNode(hierarchy[0]);
root = traverse( hierarchy, hierarchyChildOf, 0 );
treeModel = (DefaultTreeModel)tree1.getModel();
treeModel.setRoot(root);
...
}
public static DefaultMutableTreeNode traverse( Object hierarchy[], Object hierarchyChildOf[], int k ) {
for(int i = 0; i < hierarchy.length; i++) {
if (hierarchy[k].equals(hierarchyChildOf[i])) {
System.out.println( hierarchy[i] + ", " + hierarchyChildOf[i]);
DefaultMutableTreeNode child = new DefaultMutableTreeNode(hierarchy[i]);
node.add(child);
traverse(hierarchy, hierarchyChildOf, i);
}
}
return (node);
}
Thank you!