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

本文介绍了一个Java示例程序,该程序可以遍历JTree的所有节点,并以树状结构的形式打印出来。同时,还提供了ITestDataTree、ITestDataTreeNodes及ITestDataTreeNode接口的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面的例子中将获取到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(booleanordered)
Sets the tree to be compared in either an ordered or unordered fashion.

void

setTreeNodes(ITestDataTreeNodestreeNodes)
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(ITestDataTreetree)
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(booleanmasked)
Sets the masked attribute of the element of this node.

void

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

void

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

void

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值