JTree用法及JTree使用经验总结
import java.awt.Dimension;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.BoxLayout;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
/*
JTree的构造函数:
JTree()
JTree(Hashtable value)
JTree(Object[] value)//只有这个构造函数可以创建多个根结点
JTree(TreeModel newModel)
JTree(TreeNode root)
JTree(TreeNode root, boolean asksAllowsChildren)
JTree(Vector value)
*/
public class JTreeDemo
{
public static void main (String[] args)
{
// 构造函数:JTree()
JTree example1 = new JTree();
// 构造函数:JTree(Object[] value)
Object[] letters = { " a " , " b " , " c " , " d " , " e " };
JTree example2 = new JTree (letters);
// 构造函数:JTree(TreeNode root)(TreeNode空)
// 用空结点创建树
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(); // 定义树结点
JTree example3 = new JTree (node1); // 用此树结点做参数调用 JTree的构造函数创建含有一个根结点的树
// 构造函数:JTree(TreeNode root)(同上,只是TreeNode非空)
// 用一个根结点创建树
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode( " Color " );
JTree example4 = new JTree (node2); // 结点不可以颜色,默认为白面黑字
example4.setBackground (Color.lightGray);
// 构造函数:JTree(TreeNode root, boolean asksAllowsChildren)(同上,只是TreeNode又有不同)
// 使用DefaultMutableTreeNode类先用一个根结点创建树,设置为可添加孩子结点,再添加孩子结点
DefaultMutableTreeNode color = new DefaultMutableTreeNode( " Color " , true );
DefaultMutableTreeNode gray = new DefaultMutableTreeNode ( " Gray " );
color.add (gray);
color.add ( new DefaultMutableTreeNode ( " Red " ));
gray.add ( new DefaultMutableTreeNode ( " Lightgray " ));
gray.add ( new DefaultMutableTreeNode ( " Darkgray " ));
color.add ( new DefaultMutableTreeNode ( " Green " ));
JTree example5 = new JTree (color);
// 构造函数:JTree(TreeNode root)(同上,只是TreeNode非空)
// 通过逐个添加结点创建树
DefaultMutableTreeNode biology = new DefaultMutableTreeNode ( " Biology " );
DefaultMutableTreeNode animal = new DefaultMutableTreeNode ( " Animal " );
DefaultMutableTreeNode mammal = new DefaultMutableTreeNode ( " Mammal " );
DefaultMutableTreeNode horse = new DefaultMutableTreeNode ( " Horse " );
mammal.add (horse);
animal.add (mammal);
biology.add (animal);
JTree example6 = new JTree (biology);
horse.isLeaf();
horse.isRoot();
// 构造函数:JTree(TreeModel newModel)
// 用DefaultMutableTreeNodel类定义一个结点再用这个结点做参数定义一个用DefaultTreeMode
// 创建一个树的模型,再用JTree的构造函数创建一个树
DefaultMutableTreeNode root = new DefaultMutableTreeNode ( " Root1 " );
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode ( " Child1 " );
DefaultMutableTreeNode child11 = new DefaultMutableTreeNode ( " Child11 " );
DefaultMutableTreeNode child111 = new DefaultMutableTreeNode ( " Child111 " );
root.add (child1); child1.add (child11); child11.add (child111);
DefaultTreeModel model = new DefaultTreeModel (root);
JTree example7 = new JTree (model);
JPanel panel = new JPanel();
panel.setLayout ( new BoxLayout (panel, BoxLayout.X_AXIS));
panel.setPreferredSize ( ));
panel.add ( new JScrollPane (example1)); // JTree必须放在JScrollPane上
panel.add ( new JScrollPane (example2));
panel.add ( new JScrollPane (example3));
panel.add ( new JScrollPane (example4));
panel.add ( new JScrollPane (example5));
panel.add ( new JScrollPane (example6));
panel.add ( new JScrollPane (example7));
JFrame frame = new JFrame ( " JTreeDemo " );
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (panel);
frame.pack();
frame.show();
}
}
在实际开发过程中会经常使用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
JTree用法及JTree使用经验总结的更多相关文章
- JTree实现好友列表
最近学习了一下JTree的使用方法: 先来看一下树的实例: 构建一个树, DefaultMutableTreeNode root = new DefaultMutableTreeNode(" ...
- JTree使用
package JTree; import java.awt.Component; import javax.swing.Icon; import javax.swing.JTree; import ...
- Swing组件Jtree,JTablePane选项卡运用
今天开始写技术博客,说实话,本没有什么技术,说是总结也好,说是分享也罢,总之是想自己有意识的做一些事情,作为一名即将毕业的大学生,总是想以最好的状态,去面向社会,今天就是我准备好了的时候,本人将技术博 ...
- JAVA GUI学习 - JTree树结构组件学习 ***
public class JTreeKnow extends JFrame { public JTreeKnow() { this.setBounds(300, 100, 400, 500); thi ...
- JTree demo
JFrame居中方法一: setLocationRelativeTo(null); 注意:必须在整个frame初始化完成后再加上此语句,否则将显示在屏幕右下角 方法二: private Dimen ...
- Java Swing 树状组件JTree的使用方法(转)
树中特定的节点可以由 TreePath(封装节点及其所有祖先的对象)标识,或由其显示行(其中显示区域中的每一行都显示一个节点)标识.展开 节点是一个非叶节点(由返回 false 的 TreeModel ...
- 详解Swing中JTree组件的功能
JTree组件是另外一个复杂组件,它不像 JTable 那样难用,但是也不像 JList 那么容易.使用 JTree 时麻烦的部分是它要求的数据模型. JTree组件的功能来自树的概念,树有分支和叶子 ...
- java控件之树形结构JTree
import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.event.TreeSelectionEvent; im ...
- JTree常用方法
private JTree jtNetDevice;//数组件申明private JScrollPane jspTree;//滚动面板申明 1.初始化 DefaultMutableTreeNo ...
随机推荐
- OPENFILER记下,有空再玩之,ISCSI,以后网络起来了,速度还是应该可以的
- I2C转UART
I2C转UART,51单片机普通IO口模拟I2C从机,解决UART不够的问题 /************************************************************ ...
- struts2框架通过jQuery实现AJAX应用
众所周知,在web2.0时代,哪个web框架要是不跟AJAX沾点边,都不好意思说自己的框架有多么多么NB,当然struts也不例外,从 struts1开始到现在的struts2也都对AJAX有支持.A ...
- centos 6.5关闭NetworkManager
jrhmpt01:/root# rpm -qa | grep -i network NetworkManager-glib-0.8.1-99.el6.x86_64 system-config-netw ...
- bzoj3709
首先明显会想到贪心对于那些怪物回血比耗血多的,我们显然应该先打耗血少的那些回血比耗血多的怎么办呢?由于不管怎么打(假设体力负数了还能打),最终体力是一定,我们从最终体力倒推,相当于先吃药掉血,打怪物回 ...
- 加密解密,CryptoStream()的使用
一:上图 二:代码 主界面代码 using System; using System.Collections.Generic; using System.ComponentModel; using S ...
- IIS7.0出错的解决方案 IIS 状态代码:IIS详细错误代码以及解释
一.请求筛选模块被配置为拒绝包含双重转义序列的请求.HTTP 错误 404.11 - Not Found 1.单击 开始 . 在 开始搜索 框中, 键入 Notepad. 右击 记事本 , 然后单 ...
- android网络图片的下载
android网络图片的下载 /** * Get image from newwork * * @param path * The path of image * @return byte[] * @ ...
- bzoj2049: [Sdoi2008]Cave 洞穴勘测
lct入门题? 得换根了吧TAT 这大概不是很成熟的版本.. #include<iostream> #include<cstring> #include<cstdlib& ...
- MongoDB C++ 2.4.5 driver 编译安装问题
安装参考前文,http://blog.csdn.net/sheismylife/article/details/8794589 方法一致.只不过这次在GCC4.8.1上编译. scons instal ...