I want to make an app that can search a JTree by typing letters to a textfield. I want it to display words that matches the incomplete input. Like it should match the node "Sample" even if the user has only typed "Sa".
Here is my code. It is working but only for whole words. Thanks!
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.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
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 JLabel searchLabel = new JLabel("Enter Node to be searched");
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("Search Me Please"));
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(20);
searchText.getDocument().addDocumentListener(new SearchListener());
searchPanel.add(searchText);
searchPanel.add(searchLabel);
getContentPane().add(searchPanel, BorderLayout.SOUTH);
setSize(700, 400);
setVisible(true);
}
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;
}
private class SearchListener implements DocumentListener{
@Override
public void insertUpdate(DocumentEvent 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");
}
}
@Override
public void removeUpdate(DocumentEvent 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");
}
}
@Override
public void changedUpdate(DocumentEvent 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");
}
}
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 static void main(String[] arg) {
SearchTree st = new SearchTree();
}
}
Change your searchNode function to:
public DefaultMutableTreeNode searchNode(String nodeStr) {
Enumeration e = root.breadthFirstEnumeration();
while (e.hasMoreElements()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
if (node.getUserObject().toString().toLowerCase().startsWith(nodeStr.toLowerCase())) {
return node;
}
}
return null;
}
instead of using the .equals(" ") function use .startsWith(" ")
Edit: You have 2 searchNode functions, make sure you change the one in SearchListener class, the other one is useless