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

code for searchNode() is

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

I have written this code top search a node in tree? But I'm having trouble highlighting the found node with a blue color. Can you provide a solution?

Sujay
  • 6,753
  • 2
  • 30
  • 49
Nikhil
  • 2,857
  • 9
  • 34
  • 58

1 Answers1

3

Your implementation of TreeCellRenderer can specify the desired color. See Customizing a Tree's Display and the examples cited here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045