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搜索節點的Java Swing
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;
}
我寫了這個代碼頂級搜索在樹中的節點?但是我無法突出顯示找到的藍色節點。你能提供一個解決方案嗎?
我沒有試過這個,但你有沒有試過在樹上調用'repaint'? – MadProgrammer
對不起,你是什麼意思,你「不能得到那個」?不'm_tree.repaint()'工作? – MadProgrammer