2
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");
        }
    }
});

searchNode() 的代码是

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;
}

我已经写了这段代码顶部搜索树中的一个节点?但是我无法用蓝色突出显示找到的节点。你能提供一个解决方案吗?

4

1 回答 1

3

您的实现TreeCellRenderer可以指定所需的颜色。请参阅自定义树的显示和此处引用的示例。

于 2012-09-17T10:28:37.410 回答