for (Enumeration e = root.preorderEnumeration(); e.hasMoreElements() && theNode == null;) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
}
For that example above, how can you find out how deep you are in the tree branch? If you are iterating over siblings, how do you get it’s index?
You can get the depth by counting the number of parents iterating on
getParent()fromnodeuntil result isnull.You can get
node‘s index thanks tonode.getIndex(node.getParent()).If you need both information for each node you traverse, I recommend you for efficiency to write your own traversor code, either inspired from
DefaultMutableTreeNode.getNextNode()orDefaultMutableTreeNode.PreorderEnumerationinner class. In that later case, the generated stack should contain a structure{ node, treeDepth, siblingIndex }