在实际开发过程中会经常使用jtree组件,平时会遇到这样或那样的问题,在此将偶得一点经验写下来,与大家共享,希望对大家有所帮助。
private jtree jtnetdevice;//数组件申明
private jscrollpane jsptree;//滚动面板申明
1、初始化
defaultmutabletreenode rootnode = new defaultmutabletreenode("root");
jtnetdevice = new jtree(rootnode);
jtnetdevice.setautoscrolls(true);
gettreeselectionmodel().setselectionmode(treeselectionmodel.single_tree_selection);//设置单选模式
jsptree = new jscrollpane();
jsptree.getviewport().add(jtnetdevice, null);
2、三个经常使用的取值函数
private defaulttreemodel gettreemodel(){
return (defaulttreemodel)jtnetdevice.getmodel();
}
private defaultmutabletreenode getrootnode(){
return (defaultmutabletreenode)gettreemodel().getroot();
}
private treeselectionmodel gettreeselectionmodel(){
return jtnetdevice.getselectionmodel();
}
3、根据node得到path:
treepath visiblepath = new treepath(gettreemodel().getpathtoroot(node));
4、根据path展开到该节点
jtnetdevice.makevisible(visiblepath);
5、根据path设定该节点选定
jtnetdevice.setselectionpath(visiblepath);
6、选中节点的方法
首先,根据节点得到树路径,其中chosen为需要选中的节点
treepath visiblepath = new treepath( ( (defaulttreemodel) jtnetdevice.getmodel()).
getpathtoroot(chosen));
然后根据path选中该节点
jtnetdevice.setselectionpath(visiblepath);
7、滚动到可见位置
jtnetdevice.scrollpathtovisible(visiblepath);
8、给jtree添加右键弹出菜单
void jtnetdevice_mousereleased(mouseevent e) {
if (e.ispopuptrigger()) {
jpopupmenu1.show(e.getcomponent(), e.getx(), e.gety());//弹出右键菜单
}
}
9、关于jtree的展开
// if expand is true, expands all nodes in the tree.
// otherwise, collapses all nodes in the tree.
public void expandall(jtree tree, boolean expand) {
treenode root = (treenode)tree.getmodel().getroot();
// traverse tree from root
expandall(tree, new treepath(root), expand);
}
private void expandall(jtree tree, treepath parent, boolean expand) {
// traverse children
treenode node = (treenode)parent.getlastpathcomponent();
if (node.getchildcount() >= 0) {
for (enumeration e=node.children(); e.hasmoreelements(); ) {
treenode n = (treenode)e.nextelement();
treepath path = parent.pathbyaddingchild(n);
expandall(tree, path, expand);
}
}
// expansion or collapse must be done bottom-up
if (expand) {
tree.expandpath(parent);
} else {
tree.collapsepath(parent);
}
}
10、如何遍历jtree
// 创建树
jtree tree = new jtree();
// 添加树节点......
// 遍历所有节点
visitallnodes(tree);
// 仅遍历展开的节点
visitallexpandednodes(tree);
// traverse all nodes in tree
public void visitallnodes(jtree tree) {
treenode root = (treenode)tree.getmodel().getroot();
visitallnodes(root);
}
public void visitallnodes(treenode node) {
// node is visited exactly once
process(node);
if (node.getchildcount() >= 0) {
for (enumeration e=node.children(); e.hasmoreelements(); ) {
treenode n = (treenode)e.nextelement();
visitallnodes(n);
}
}
}
// traverse all expanded nodes in tree
public void visitallexpandednodes(jtree tree) {
treenode root = (treenode)tree.getmodel().getroot();
visitallexpandednodes(tree, new treepath(root));
}
public void visitallexpandednodes(jtree tree, treepath parent) {
// return if node is not expanded
if (!tree.isvisible(parent)) {
return;
}
// node is visible and is visited exactly once
treenode node = (treenode)parent.getlastpathcomponent();
process(node);
// visit all children
if (node.getchildcount() >= 0) {
for (enumeration e=node.children(); e.hasmoreelements(); ) {
treenode n = (treenode)e.nextelement();
treepath path = parent.pathbyaddingchild(n);
visitallexpandednodes(tree, path);
}
}
}
posted on 2006-04-04 17:24
simone 阅读(46088)
评论(5) 所属分类:
java