I've got a JTree, which I am trying to search. I've written a quick recursive search function. The function takes a parent/child node name pair as a string.
private void RecursiveSearch(javax.swing.tree.DefaultMutableTreeNode node, java.util.ArrayList<TreeNode> nodelist, java.lang.String destination, java.lang.String origin) {
nodelist.add(node);
Controller.TreeData parentdata = (Controller.TreeData)node.getUserObject();
for(int i = 0; i < node.getChildCount(); i++) {
javax.swing.tree.DefaultMutableTreeNode childnode = (javax.swing.tree.DefaultMutableTreeNode)node.getChildAt(i);
Controller.TreeData childdata = (Controller.TreeData)childnode.getUserObject();
if (parentdata.GetName().trim().toUpperCase().equals(origin) && childdata.GetName().trim().toUpperCase().equals(destination)) {
nodelist.add(childnode);
return;
}
}
// We didn't find it. Recurse.
for(int i = 0; i < node.getChildCount(); i++) {
RecursiveSearch((javax.swing.tree.DefaultMutableTreeNode)node.getChildAt(i), nodelist, destination, origin);
}
nodelist.remove(node);
}
However, it's not returning values when it should be. I got the root node from the TreeModel and the array starts out empty. I checked the JTree and the TreeModel and neither of them seem to offer any sort of searching functionality. Any suggestions?
Edit: I'm not going to try to explain my original function (it was originally written in another language). But I replaced it with this:
javax.swing.tree.DefaultMutableTreeNode rootnode = (javax.swing.tree.DefaultMutableTreeNode)datatree.getModel().getRoot();
java.util.Enumeration nodeenum = rootnode.breadthFirstEnumeration();
while(nodeenum.hasMoreElements()) {
javax.swing.tree.DefaultMutableTreeNode nextnode = (javax.swing.tree.DefaultMutableTreeNode)nodeenum.nextElement();
Controller.TreeData data = (Controller.TreeData)nextnode.getUserObject();
javax.swing.tree.DefaultMutableTreeNode parentnode = (javax.swing.tree.DefaultMutableTreeNode)nextnode.getParent();
Controller.TreeData parentdata = (Controller.TreeData)(parentnode.getUserObject());
if (parentdata.GetName().trim().toUpperCase().equals(origin) && data.GetName().trim().toUpperCase().equals(destination)) {
datatree.setSelectionPath(new javax.swing.tree.TreePath(treemodel.getPathToRoot(nextnode)));
return;
}
}
javax.swing.JOptionPane.showMessageDialog(primaryframe, "Could not find the requested depots");
However, it doesn't actually seem to find anything. I started with the root node, so it should enumerate the whole tree. Fixed the null pointer exception bug in this version.
void
function. Are you trying to have it modify the JTree in some way? What behavior are you trying to achieve? – Nate W.