我有一个包含100个节点的jtree。现在我想从该树中搜索特定节点并使该节点扩展..? 我怎样才能解决这个问题。?
答案 0 :(得分:24)
扩展@ mKorbel的答案,并在How to Use Trees中讨论,您可以递归搜索TreeModel
并获得TreePath
到结果节点。获得所需的path
后,很容易在树中显示它。
tree.setSelectionPath(path);
tree.scrollPathToVisible(path);
附录:这是“获得TreePath
。”的一种方式。
private TreePath find(DefaultMutableTreeNode root, String s) {
@SuppressWarnings("unchecked")
Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
while (e.hasMoreElements()) {
DefaultMutableTreeNode node = e.nextElement();
if (node.toString().equalsIgnoreCase(s)) {
return new TreePath(node.getPath());
}
}
return null;
}
答案 1 :(得分:2)
我假设你想要找到一个带有特定字符串的节点,对吗?其他答案解释了使用枚举方法做到这一点的方法......(我确信他们都知道在现实世界中你还必须满足多个节点拥有的可能性寻求的字符串等。)
但也有其他更性感的方式。例如,如果您将所有节点放入某种collection
(ArrayList
等*)中,因为它们已插入树中(并在删除它们时将其删除,包括明确删除所有节点后代)...如果你也实现了一些东西,那么两个节点被视为&#34;相等&#34;如果他们从toString
获得了相同的结果(或实现了Comparator
,那么你可以轻松地弹出匹配的ArrayList
中的实际节点(或节点),以及然后去
tree.expandPath( new TreePath( node_found.getPath())
树的一个要点是它真的是节点的路径(有时称为&#34;面包屑&#34;)这是真正的&#34;身份&#34 ;任何给定节点。就显示的String
值而言,这意味着您可能在同一个树中:
路径:&#34;彼得&#34; - &#34; Piper&#34; - &#34;腌制&#34; - &#34;胡椒&#34;
路径:&#34;烹饪专业&#34; - &#34;香料&#34; - &#34;胡椒&#34;
路径:&#34;我的最爱&#34; - &#34;食物&#34; - &#34;调味品&#34; - &#34;胡椒&#34;
所以说你想要搜索,然后选择或突出显示其中一个&#34; pepper&#34;节点......拥有一个强大的力量并不是非常有效率。沿着这条路径的每个元素的枚举方法(树越大,问题就越严重)。
使用我的建议变得非常简单:只需将你的&#34;面包屑分开&#34;路径,从根开始,或者在任何地方,然后,当您深入到树中时,在&#34;更高的&#34;上使用node.isNodeDescendant()
。你已经找到的节点(即那些远离root的节点)(这里是3&#34; pepper&#34;节点):如果你想要上面的第一条路径,你首先会找到节点&#34; Peter& #34;,然后立即发现唯一的&#34;辣椒&#34;可以满足isNodeDescendant
测试的节点将产生您正在寻找的整个路径。
*当然,某种形式的散列集合会更有效率。但只有当树中有数千个或更多节点时,才需要考虑这一点。
答案 2 :(得分:1)
以下是如何在搜索中循环遍历树的示例:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
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.TreePath;
public class TreeDemo extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
public JTree tree;
public JButton button;
public JTextField text;
public TreeDemo() {
button = new JButton("Enter search text below and click");
text = new JTextField();
button.addActionListener(this);
tree = new JTree();
DefaultMutableTreeNode root = new DefaultMutableTreeNode( "Deck" );
DefaultMutableTreeNode itemClubs= new DefaultMutableTreeNode( "Clubs" );
addAllCard( itemClubs );
root.add( itemClubs );
DefaultMutableTreeNode itemDiamonds = new DefaultMutableTreeNode( "Diamonds" );
addAllCard( itemDiamonds );
root.add( itemDiamonds );
DefaultMutableTreeNode itemSpades = new DefaultMutableTreeNode( "Spades" );
addAllCard( itemSpades );
root.add( itemSpades );
DefaultMutableTreeNode itemHearts = new DefaultMutableTreeNode( "Hearts" );
addAllCard( itemHearts );
root.add( itemHearts );
DefaultTreeModel treeModel = new DefaultTreeModel( root );
tree = new JTree( treeModel );
JScrollPane scrollPane = new JScrollPane(tree);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(button, BorderLayout.NORTH);
getContentPane().add(text, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(375, 400);
}
public void addAllCard( DefaultMutableTreeNode suit ) {
suit.add( new DefaultMutableTreeNode( "Ace" ) );
suit.add( new DefaultMutableTreeNode( "Two" ) );
suit.add( new DefaultMutableTreeNode( "Three" ) );
suit.add( new DefaultMutableTreeNode( "Four" ) );
suit.add( new DefaultMutableTreeNode( "Five" ) );
suit.add( new DefaultMutableTreeNode( "Six" ) );
suit.add( new DefaultMutableTreeNode( "Seven" ) );
suit.add( new DefaultMutableTreeNode( "Eight" ) );
suit.add( new DefaultMutableTreeNode( "Nine" ) );
suit.add( new DefaultMutableTreeNode( "Ten" ) );
suit.add( new DefaultMutableTreeNode( "Jack" ) );
suit.add( new DefaultMutableTreeNode( "Queen" ) );
suit.add( new DefaultMutableTreeNode( "King" ) );
}
public final DefaultMutableTreeNode findNode(String searchString) {
List<DefaultMutableTreeNode> searchNodes = getSearchNodes((DefaultMutableTreeNode)tree.getModel().getRoot());
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
DefaultMutableTreeNode foundNode = null;
int bookmark = -1;
if( currentNode != null ) {
for(int index = 0; index < searchNodes.size(); index++) {
if( searchNodes.get(index) == currentNode ) {
bookmark = index;
break;
}
}
}
for(int index = bookmark + 1; index < searchNodes.size(); index++) {
if(searchNodes.get(index).toString().toLowerCase().contains(searchString.toLowerCase())) {
foundNode = searchNodes.get(index);
break;
}
}
if( foundNode == null ) {
for(int index = 0; index <= bookmark; index++) {
if(searchNodes.get(index).toString().toLowerCase().contains(searchString.toLowerCase())) {
foundNode = searchNodes.get(index);
break;
}
}
}
return foundNode;
}
private final List<DefaultMutableTreeNode> getSearchNodes(DefaultMutableTreeNode root) {
List<DefaultMutableTreeNode> searchNodes = new ArrayList<DefaultMutableTreeNode>();
Enumeration<?> e = root.preorderEnumeration();
while(e.hasMoreElements()) {
searchNodes.add((DefaultMutableTreeNode)e.nextElement());
}
return searchNodes;
}
public static void main(String[] args) {
TreeDemo app = new TreeDemo();
app.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String search = text.getText();
if(search.trim().length() > 0 ) {
DefaultMutableTreeNode node = findNode(search);
if( node != null ) {
TreePath path = new TreePath(node.getPath());
tree.setSelectionPath(path);
tree.scrollPathToVisible(path);
}
}
}
}