问题
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.
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".
Thank you so much for your input . I am trapped here for days . =(
Here is my 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();
}
}
回答1:
I think that's what you were looking for:
Edit 4:
import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.BorderFactory;
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.WindowConstants;
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 final DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
private final DefaultTreeModel model = new DefaultTreeModel(root);
private final JTree tree = new JTree(model);
private final JLabel searchLabel = new JLabel("Enter Node to be searched");
private final JTextField searchText;
private final ArrayList<DefaultMutableTreeNode> initNodes = new ArrayList<>();
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"));
initNodes.add(n1);
initNodes.add(n2);
initNodes.add(n3);
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);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
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);
initNodes.remove(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 {
boolean expandIt = false;
@Override
public void insertUpdate(DocumentEvent e) {
documentListener();
}
@Override
public void removeUpdate(DocumentEvent e) {
documentListener();
}
@Override
public void changedUpdate(DocumentEvent e) {
documentListener();
}
private void documentListener() {
ArrayList<DefaultMutableTreeNode> list = searchNode(searchText.getText());
root.removeAllChildren();
for (DefaultMutableTreeNode mtn : list) {
root.add(mtn);
}
model.reload(root);
if (expandIt) {
for (int i = 0; i < tree.getRowCount(); i++) {
tree.expandRow(i);
}
}
}
public ArrayList<DefaultMutableTreeNode> searchNode(String nodeStr) {
if (nodeStr == null || nodeStr.isEmpty()) {
expandIt = false;
return initNodes;
}
expandIt = true;
ArrayList<DefaultMutableTreeNode> list = new ArrayList<>();
for (DefaultMutableTreeNode mtn : initNodes) {
if (checkText(mtn, nodeStr)) {
list.add(mtn);
continue;
}
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) mtn.clone();
for (int i = 0; i < mtn.getChildCount(); i++) {
DefaultMutableTreeNode child = (DefaultMutableTreeNode) mtn.getChildAt(i);
if (checkText(child, nodeStr)) {
parent.add((DefaultMutableTreeNode) child.clone());
}
}
if (parent.getChildCount() > 0) {
list.add(parent);
}
}
return list;
}
public boolean checkText(DefaultMutableTreeNode node, String text) {
return node.getUserObject().toString().toLowerCase().startsWith(text.toLowerCase());
}
}
public static void main(String[] arg) {
SearchTree st = new SearchTree();
}
}
来源:https://stackoverflow.com/questions/30113507/how-to-limit-the-display-of-a-searched-node-in-a-jtree-to-itself-and-its-parents