Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java swing中查找下一个出现的树节点_Java_Swing_Jtree - Fatal编程技术网

在java swing中查找下一个出现的树节点

在java swing中查找下一个出现的树节点,java,swing,jtree,Java,Swing,Jtree,我有一棵树。我已经编写了代码来搜索树中的给定节点,当单击搜索按钮时,现在我必须再次单击该按钮来搜索下一个事件(如果存在)?你能帮忙吗? 搜索按钮的代码为 m_searchButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode node = searchNode(m_searchText.getText());

我有一棵树。我已经编写了代码来搜索树中的给定节点,当单击搜索按钮时,现在我必须再次单击该按钮来搜索下一个事件(如果存在)?你能帮忙吗? 搜索按钮的代码为

 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;

}

返回找到的节点列表,而不是只返回一个节点

public List<DefaultMutableTreeNode> searchNode(String nodeStr) {
DefaultMutableTreeNode node = null;
Enumeration e = m_rootNode.breadthFirstEnumeration();
List<DefaultMutableTreeNode> list = new ArrayList<DefaultMutableTreeNode>();
while (e.hasMoreElements()) {
  node = (DefaultMutableTreeNode) e.nextElement();
  if (nodeStr.equals(node.getUserObject().toString())) {
    list.add(node);
  }
}
return list;
}
公共列表搜索节点(字符串节点){
DefaultMutableTreeNode=null;
枚举e=m_rootNode.breadthfirstumeration();
列表=新的ArrayList();
而(e.hasMoreElements()){
节点=(DefaultMutableTreeNode)e.nextElement();
if(nodeStr.equals(node.getUserObject().toString()){
列表。添加(节点);
}
}
退货清单;
}
自己做按钮
ActionListener
的逻辑,这并不难


添加节点列表作为类成员,当您单击按钮时,检查它是否为空,如果为空,检索列表,获取第一个节点;你想用它做什么就做什么,然后从列表中删除它。当到达最后一个元素时,再次将列表设置为null

没有理由向util.List添加节点,因为所有元素都存储在DefaultTreeModel中,@mKorbel:Maybe
List
Map
到找到的节点以供以后导航?