Java: TreeModel recursively searching through UserObject field?

I have a Jtree using DefaultTreeModel, each individual node contains a UserObject containing various string fields.

I would like to find and select the node, doing a recursive traversal, until I find DefaultMutableTreeNode with a UserObject matching one of its fields, and programmatically select node.

Are there any examples related to searching through DefaultMutableTreeNode UserObject fields?

+4
source share
1 answer

DefaultMutableTreeNode has depthFirstEnumeration () and widththFirstEnumeration () . Call the one you want to iterate through the enumeration until you find the node that has the UserObject that you want.

 DefaultMutableTreeNode theNode = null; for (Enumeration e = root.depthFirstEnumeration(); e.hasMoreElements() && theNode == null;) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); if (whatIWantIs(node.getUserObject)) { theNode = node; } } 
+10
source

Source: https://habr.com/ru/post/1399672/


All Articles