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;
}
我已经编写了这段代码,顶部搜索树中的节点?但我无法用蓝色突出显示找到的节点。能提供一下解决方案吗?
最佳答案
您的TreeCellRenderer
实现可以指定所需的颜色。请参阅Customizing a Tree's Display以及引用的例子here .
关于java - 在java swing中突出显示树中搜索到的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12456735/