title: How to limit the display of a Searched Node in a JTree to itself and its parents (other nodes will be excluded in the display)?
body: <p>I want to display the searched node and its parents only. I wanted to exclude the unrelated nodes. My code only does search and display but it does not filter out the other nodes. They are still in the display.</p>
  
  <p>What approach can be done to do this? You can get what I mean by going the Eclipse's Run Configurations' Search function. It only displays what is searched and it's "parents".
  <img src="https://i.stack.imgur.com/CGjOE.png" alt="enter image description here"></p>
  
  <p>Thank you so much for your input . I am trapped here for days . =(</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.util.Enumeration;
  
  import javax.swing.BorderFactory;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTextField;
  import javax.swing.JTree;
  import javax.swing.tree.DefaultMutableTreeNode;
  import javax.swing.tree.DefaultTreeModel;
  import javax.swing.tree.MutableTreeNode;
  import javax.swing.tree.TreeNode;
  import javax.swing.tree.TreePath;
  
  public class SearchTree extends JFrame {
  
      private DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
  
      private DefaultTreeModel model = new DefaultTreeModel(root);
  
      private JTree tree = new JTree(model);
  
      private JButton searchButton = new JButton("Search Node");
  
      private JTextField searchText;
  
      public SearchTree() {
          DefaultMutableTreeNode n1 = new DefaultMutableTreeNode("Sample 1");
          n1.add(new DefaultMutableTreeNode("2nd level: Child l"));
          n1.add(new DefaultMutableTreeNode("Search Me"));
          n1.add(new DefaultMutableTreeNode("Bob"));
  
          DefaultMutableTreeNode n2 = new DefaultMutableTreeNode("Find me");
          n2.add(new DefaultMutableTreeNode("2nd level: Child 2"));
          n2.add(new DefaultMutableTreeNode("Peter"));
          n2.add(new DefaultMutableTreeNode("Lily"));
  
          DefaultMutableTreeNode n3 = new DefaultMutableTreeNode("Explore me");
          n3.add(new DefaultMutableTreeNode("2nd level: Child 3"));
          n3.add(new DefaultMutableTreeNode("Rome"));
          n3.add(new DefaultMutableTreeNode("Italy"));
  
          root.add(n1);
          root.add(n2);
          root.add(n3);
  
          tree.setEditable(true);
          tree.setSelectionRow(0);
  
          JScrollPane scrollPane = new JScrollPane(tree);
          getContentPane().add(scrollPane, BorderLayout.CENTER);
  
          JPanel searchPanel = new JPanel();
          searchPanel.setBorder(BorderFactory.createEtchedBorder());
  
          searchText = new JTextField(10);
          searchPanel.add(searchText);
  
          searchButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  DefaultMutableTreeNode node = searchNode(searchText.getText());
                  if (node != null) {
                      TreeNode[] nodes = model.getPathToRoot(node);
                      TreePath path = new TreePath(nodes);
                      tree.scrollPathToVisible(path);
                      tree.setSelectionPath(path);
                  } else {
                      System.out.println("Node with string "
                              + searchText.getText() + " not found");
                  }
              }
          });
          searchPanel.add(searchButton);
  
          getContentPane().add(searchPanel, BorderLayout.SOUTH);
          setSize(700, 400);
          setVisible(true);
  
      }
  
      public DefaultMutableTreeNode searchNode(String nodeStr) {
          DefaultMutableTreeNode node = null;
          Enumeration e = root.breadthFirstEnumeration();
          while (e.hasMoreElements()) {
              node = (DefaultMutableTreeNode) e.nextElement();
              if (nodeStr.equals(node.getUserObject().toString())) {
                  return node;
              }
          }
          return null;
      }
  
      public void removeNode(DefaultMutableTreeNode selNode) {
          if (selNode == null) {
              return;
          }
          MutableTreeNode parent = (MutableTreeNode) (selNode.getParent());
          if (parent == null) {
              return;
          }
          MutableTreeNode toBeSelNode = getSibling(selNode);
          if (toBeSelNode == null) {
              toBeSelNode = parent;
          }
          TreeNode[] nodes = model.getPathToRoot(toBeSelNode);
          TreePath path = new TreePath(nodes);
          tree.scrollPathToVisible(path);
          tree.setSelectionPath(path);
          model.removeNodeFromParent(selNode);
      }
  
      private MutableTreeNode getSibling(DefaultMutableTreeNode selNode) {
          MutableTreeNode sibling = (MutableTreeNode) selNode
                  .getPreviousSibling();
          if (sibling == null) {
              sibling = (MutableTreeNode) selNode.getNextSibling();
          }
          return sibling;
      }
  
      public static void main(String[] arg) {
          SearchTree st = new SearchTree();
      }
  }
  </code></pre>