我有一个jtree。我已经编写了代码以在单击搜索按钮时搜索树中的给定节点,现在我必须通过再次单击该按钮来搜索下一个出现是否存在?你能帮我吗?搜索按钮的代码是
m_searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode node = searchNode(m_searchText.getText());
if (node != null) {
TreeNode[] nodes = m_model.getPathToRoot(node);
TreePath path = new TreePath(nodes);
m_tree.scrollPathToVisible(path);
m_tree.setSelectionPath(path);
} else {
System.out.println("Node with string " + m_searchText.getText() + " not found");
}
}
});
搜索方法的代码是
public DefaultMutableTreeNode searchNode(String nodeStr) {
DefaultMutableTreeNode node = null;
Enumeration e = m_rootNode.breadthFirstEnumeration();
while (e.hasMoreElements()) {
node = (DefaultMutableTreeNode) e.nextElement();
if (nodeStr.equals(node.getUserObject().toString())) {
return node;
}
}
return null;
}