如何遍历JTree的每一个节点

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何遍历JTree的每一个节点相关的知识,希望对你有一定的参考价值。

参考技术A 下面的例子中将获取到JTree中的每一个节点并按树状结构打印出来:

public void testMain(Object[] args)

//Turn off Log Viewer for this example
setOption(IOptionName.BRING_UP_LOGVIEWER, false);

//Start Classics Java Application
startApp("ClassicsJavaA");

// Frame: ClassicsCD
tree2().waitForExistence();

//Display available test data types available from tree
System.out.println ("Available Tree Data Types: " + tree2().getTestDataTypes());

//Declare variables for tree
ITestDataTree cdTree;
ITestDataTreeNodes cdTreeNodes;
ITestDataTreeNode[] cdTreeNode;

//Variables to hold tree data
cdTree = (ITestDataTree)tree2().getTestData("tree");
cdTreeNodes = cdTree.getTreeNodes();
cdTreeNode = cdTreeNodes.getRootNodes();

//Print out total number of nodes
System.out.println ("Tree Total Node Count: " + cdTreeNodes.getNodeCount());
System.out.println ("Tree Root Node Count : " + cdTreeNodes.getRootNodeCount());

//Iterate through tree branches; this is a recursive method.
for (int i = 0;i<cdTreeNode.length;++i)
showTree(cdTreeNode[i], 0);

//Shut down Classics Java Application
classicsJava(ANY,MAY_EXIT).close();


void showTree(ITestDataTreeNode node, int indent)

//Recursive method to print out tree nodes with proper indenting.

//Determine number of tabs to use - to properly indent tree
int tabCount = ( indent < tabs.length() ? indent :
tabs.length() );

//Print out node name + number of children
System.out.println(tabs.substring(0, tabCount) + node.getNode() + " (" + node.getChildCount() + "children)" );

//Determine if node has children; recursively call this same
//method to print out child nodes.
ITestDataTreeNode[] children = node.getChildren();
int childCount = ( children != null ? children.length : 0 );
for ( int i = 0; i < childCount; ++i )
showTree(children[i], indent+1);


//String of tabs used to indent tree view
final String tabs = "/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t";

输出:
Available Tree Data Types: selected=选中的树层次结构, tree=树层次结构
Tree Total Node Count: 20
Tree Root Node Count : 1
Composers (5children)
Schubert (3children)
String Quartets Nos. 4 & 14 (0children)
Die schone Mullerin, Op. 25 (0children)
Symphonies Nos. 5 & 9 (0children)
Haydn (3children)
Symphonies Nos. 99 & 101 (0children)
Symphonies Nos. 94 & 98 (0children)
Violin Concertos (0children)
Bach (2children)
Brandenburg Concertos Nos. 1 & 3 (0children)
Violin Concertos (0children)
Beethoven (3children)
Symphony No. 7 (0children)
Symphony No. 9 (0children)
Symphony No. 5 (0children)
Mozart (3children)
Symphony No. 34 (0children)
Symphony in C, No. 41: Jupiter (0children)
Concerto in D for Piano (0children)

关于ITestDataTree 、 ITestDataTreeNodes 、ItestDataTreeNode的使用方法可参考RFT的帮助文档:

ITestDataTree
All Superinterfaces:
ITestData
public interface ITestDataTree
extends ITestData

The base-level tree verification-point interface. This interface represents a property set and entry points to the tree nodes.
Since:
RFT1.0

Method Summary

boolean

getOrdered()
Returns the setting of the Ordered property.

ITestDataTreeNodes

getTreeNodes()
Returns the nodes of a tree structure.

void

setOrdered(boolean ordered)
Sets the tree to be compared in either an ordered or unordered fashion.

void

setTreeNodes(ITestDataTreeNodes treeNodes)
Sets the nodes of a tree structure.本回答被提问者和网友采纳

如何在jtree中搜索特定节点并使该节点展开。?

【中文标题】如何在jtree中搜索特定节点并使该节点展开。?【英文标题】:How to search a particular node in jtree and make that node expanded.? 【发布时间】:2012-01-02 21:10:21 【问题描述】:

我有一个有 100 个节点的 jtree。现在我想从该树中搜索特定节点并使该节点扩展..? 我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

我假设您的意思是要查找具有特定字符串的节点,对吧?其他答案解释了使用枚举方法执行此操作的方法......(我相信他们都知道,在现实世界中,您还必须满足多个节点具有所寻求的字符串的可能性,等等)

但可能还有其他更性感的方式来做这件事。例如,如果您将所有节点放在某种collection 中(ArrayList 等*),因为它们被插入到树中(并在它们被删除时删除它们,包括显式删除它们的所有后代)。 . 并且如果您还实现了一些事情,如果两个节点从toString 获得相同的结果(或实现了这样做的Comparator),则它们被视为“相等”,那么您可以轻松弹出实际节点(或节点) 在ArrayList 里面匹配,然后去

tree.expandPath( new TreePath( node_found.getPath())

树的一个要点是它实际上是节点的路径(有时称为“面包屑”),它是任何给定节点的真正“身份”。就显示的String 值而言,这意味着您可能在同一棵树中拥有:

路径:“彼得”-“派珀”-“腌制”-“胡椒” 路径:“烹饪特色”-“香料”-“胡椒” 路径:“我的最爱”-“食物”-“调味品”-“胡椒”

假设您要搜索,然后选择或突出显示这些“胡椒”节点之一……为每个元素采用“蛮力”枚举方法并不是很有效路径(树越大,问题当然越严重)。

使用我的建议,它变得非常简单:只需从根或任何位置开始划分您的“面包屑”路径,然后,当您深入到树中时,在“更高”节点上使用 node.isNodeDescendant() )(即那些离根更远的)你已经找到了(这里是 3 个“胡椒”节点):如果你想要上面的第一个路径,你会首先找到节点“彼得”,然后紧接着唯一的“胡椒”可以满足isNodeDescendant 测试的节点将产生您正在寻找的整个路径。

* 当然,某种形式的散列集合会更有效。但只有在树中有数千个或更多节点时才需要考虑这一点。

【讨论】:

【参考方案2】:

这是一个如何在搜索中循环遍历树的示例:

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

【讨论】:

好例子;也可以考虑text.addActionListener(this);另见Initial Threads【参考方案3】:

扩展@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;

【讨论】:

我的(狂野的 :-) 猜测是“获得 TreePath”是问题的核心——不是火箭科学,但在教程中没有这样做的例子(或者我是盲人, 非零概率 )

以上是关于如何遍历JTree的每一个节点的主要内容,如果未能解决你的问题,请参考以下文章

JAVA 得到jtree 某节点下的子节点

JTree的使用(总结,非常简洁)

java组件JTree问题,怎么给树的子节点注册鼠标双击监听

二叉树基础(上):如何遍历二叉树?

算法问题如何判断链表有环

算法问题如何判断链表有环