你试验一下下面几个方法,从文档上找的,都是DefaultMutableTreeNode下的方法:
public Enumeration children();
Creates and returns a forward-order enumeration of this node's children. Modifying this node's child array invalidates any child enumerations created before the modification. Enumeration depthFirstEnumeration()
Creates and returns an enumeration that traverses the subtree rooted at this node in depth-first order. Enumeration breadthFirstEnumeration()
Creates and returns an enumeration that traverses the subtree rooted at this node in breadth-first order. 这三个方法应该能够满足你的需求
只有这三个方法能够一次性返回末节点的所有子节点
public Enumeration children();
Creates and returns a forward-order enumeration of this node's children. Modifying this node's child array invalidates any child enumerations created before the modification. Enumeration depthFirstEnumeration()
Creates and returns an enumeration that traverses the subtree rooted at this node in depth-first order. Enumeration breadthFirstEnumeration()
Creates and returns an enumeration that traverses the subtree rooted at this node in breadth-first order. 这三个方法应该能够满足你的需求
只有这三个方法能够一次性返回末节点的所有子节点
不行呀。
JTree的遍历可以查文档Enumeration 选择广度优先或深度优先,如 jackkui(长空铸剑)
public void setSelection(String nodename) {
DefaultMutableTreeNode myroot = (DefaultMutableTreeNode) treemodel.getRoot();
Enumeration e = myroot.breadthFirstEnumeration();
DefaultMutableTreeNode node = new DefaultMutableTreeNode(nodename);
String s;
while (e.hasMoreElements()) {
node = (DefaultMutableTreeNode) e.nextElement();
s = node.toString();
if (s.equals(nodename)) {
mytree.setSelectionPath(new TreePath(((DefaultMutableTreeNode) node).getPath()));
mytree.scrollPathToVisible(new TreePath(((DefaultMutableTreeNode) node).getPath()));
} else {
break;
}
}
}以前做的希望有帮助!
int i;
int j = _node.getChildCount(); for (i = 0; i < j; i++) {
node = (DefaultMutableTreeNode) _node.getChildAt(i);
if (node.isLeaf() == false) {
mytree.expandPath(new TreePath(((DefaultMutableTreeNode) node).getPath()));
}
expand(node);
}
node = (DefaultMutableTreeNode) node.getParent();
} //endof expand public void collapse() {
DefaultMutableTreeNode myroot = (DefaultMutableTreeNode) mytree.getLastSelectedPathComponent();
Enumeration e = myroot.depthFirstEnumeration();
while (e.hasMoreElements()) {
DefaultMutableTreeNode co_node = (DefaultMutableTreeNode) e.nextElement();
if (co_node.isLeaf() == false) {
mytree.collapsePath(new TreePath(((DefaultMutableTreeNode) co_node).getPath()));
}
}
}//end of collapse
谢谢你的回复!
你说 “建立一个JTree的遍历,再通过next循环判断get那个node的path再getpathcount()判断==3,再把得到的节点存到一个什么容器里。”
但是 public void expand(DefaultMutableTreeNode _node)
和 public void collapse() 这两个方法 好象没有什么用呀。