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 ...
随机推荐
- OTP语音芯片和掩模语音芯片(mask)的区别
OTP(One Time Programable)是MCU的一种存储器类型,意思是一次性可编程:程序烧入IC后,将不可再次更改和:因此OTP语音芯片就是指一次性烧录的语音IC. 从OTP定义上来看,只 ...
- 有趣的TWinControl.RecreateWnd,并分析在哪些场合使用
CM_RECREATEWND = CM_BASE + 51; // TWinControl里有对应函数procedure CMRecreateWnd(var Message: TMessage); m ...
- autotrace显示Statistics很多信息为0(转)
一朋友使用autotrace查看数据库执行计划发现结果如下,Statistics中很多信息为0,这个肯定是不正常现象,什么都可以为0,consistent gets也不可能为0. SQL> se ...
- BZOJ 1093 [ZJOI2007]最大半连通子图
1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1986 Solved: 802[Submit][St ...
- java程序:set改造成map
逻辑: set是无序不重复数据元素的集合. map是另一种set,如果将<key,value>看成一个整体的话,其实就是set.在map中,若用map的keyset ...
- QTP关于AOM的Javascript启动方式
序 QTP的AOM模型想必大家都很熟悉了,平时常用的就是通过VBS脚本的方式编写启动程序(也是我现在用的方法).其实,还有很多其他的方式,如Java,C#,JS,这些语言都是通过调用QTObjectM ...
- 数学概念——I - 数论,线性方程
I - 数论,线性方程 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- 第一个嵌入汇编的C程序
最近两天开始学习linux内核,看了赵炯博士的<linux内核完全注释>感觉受益匪浅.今天看到第三章 嵌入汇编部分,于是写了一个小程序试验了一下,用gcc编译通过.代码如下: #inclu ...
- Android应用开发学习之画廊视图
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 画廊视图Gallery能按水平方向显示一组图片,并可以拖动图片.下面我们来看一个使用画廊视图的例子,其运行效果如下: ...
- Jupyter Notebook 对LaTeX 的支持
Jupyter的Markdown模式比我原来想想的更加强大:它支持LaTeX! 支持的特性不多,应该能满足一般的需求了.