如何遍历JTree的每一个节点?

时间:2021-11-05 15:50:38

下面的例子中将获取到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.

 

ITestDataTreeNodes


public interface ITestDataTreeNodes

Enables an appropriate display object to be associated with the tree nodes. This is the container class for the Nodes of a tree-type verification point. Without this container class, the generic property display does not know how to display the nodes.

Since:

RFT1.0


Method Summary

 int

getNodeCount()
          Returns the number of nodes in the tree hierarchy.

 int

getRootNodeCount()
          Returns the number of root nodes.

 ITestDataTreeNode[]

getRootNodes()
          Returns the root nodes of a tree structure.

 ITestDataTree

getTree()
          This method returns the reference to tree.

 void

setRootNodes(ITestDataTreeNode[] rootNodes)
          Sets the root nodes of a tree structure.

 void

setTree(ITestDataTree tree)
          This method sets the reference to tree.

 

ITestDataTreeNode


public interface ITestDataTreeNode

Provides the necessary methods for representing a node in a Tree test-data object.

Since:

RFT1.0


Method Summary

 int

getChildCount()
          Returns the number of child nodes from this node.

 ITestDataTreeNode[]

getChildren()
          Returns the child nodes relative to this node.

 boolean

getMasked()
          Returns
true if this node in the tree should be masked from comparison in a verification-point replay.

 java.lang.Object

getNode()
          Returns a description of the node itself.

 ITestDataTreeNode

getParent()
          Returns the parent node of a tree structure.

 ITestDataTree

getTree()
          This method returns the reference to tree.

 void

setChildren(ITestDataTreeNode[] children)
          Sets the child nodes of this node.

 void

setMasked(boolean masked)
          Sets the masked attribute of the element of this node.

 void

setNode(java.lang.Object node)
          Sets a description of the node itself.

 void

setParent(ITestDataTreeNode parent)
          Sets the parent node of a tree structure.

 void

setTree(ITestDataTree tree)
          This method sets the reference to tree.