永发信息网

如何遍历JTree的每一个节点

答案:1  悬赏:80  手机版
解决时间 2021-03-26 04:19
  • 提问者网友:贪了杯
  • 2021-03-25 17:45
如何遍历JTree的每一个节点
最佳答案
  • 五星知识达人网友:詩光轨車
  • 2021-03-25 18:07
下面的例子中将获取到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 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.
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯