如何遍历JTree的每一个节点
1个回答
2015-06-22 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517181
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
下面的例子中将获取到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.
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.
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询