××××××××××××××××××××××××××××××××××××××××××××××
在实际开发过程中会经常使用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);
}
}
}
http://www.blogjava.net/wangxinsh55/archive/2006/04/04/39219.html