1 public class JTreeKnow extends JFrame 2 { 3 public JTreeKnow() 4 { 5 this.setBounds(300, 100, 400, 500); 6 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 7 8 //创建树的第一种方式 9 JTree jTree = new JTree(); 10 this.add(jTree); 11 12 //创建树的第二种方式:推荐 13 //创建根节点 14 DefaultMutableTreeNode defaultMutableTreeNode = new DefaultMutableTreeNode("根节点"); 15 //创建树模型 16 DefaultTreeModel defaultTreeModel = new DefaultTreeModel(defaultMutableTreeNode); 17 //创建树 18 JTree jTree2 = new JTree(defaultTreeModel); 19 //创建滚动面板 20 JScrollPane jScrollPane = new JScrollPane(jTree2); 21 this.add(jScrollPane); 22 //添加子节点 23 for(int i = 1; i < 3; i++) 24 { 25 DefaultMutableTreeNode defaultMutableTreeNode2 = new DefaultMutableTreeNode("第一阶子节点"+i); 26 for(int j = 1; j < 4; j++) 27 { 28 DefaultMutableTreeNode defaultMutableTreeNode3 = new DefaultMutableTreeNode("第二阶子节点"+i+j); 29 defaultMutableTreeNode2.add(defaultMutableTreeNode3); 30 } 31 defaultMutableTreeNode.add(defaultMutableTreeNode2); 32 } 33 34 //获取根节点 35 DefaultMutableTreeNode defaultMutableTreeNode2 = (DefaultMutableTreeNode) defaultTreeModel.getRoot(); 36 //根据父节点索引+子节点索引找到节点 37 DefaultMutableTreeNode defaultMutableTreeNode3 = (DefaultMutableTreeNode) defaultTreeModel.getChild(defaultMutableTreeNode2, 0); 38 System.out.println(defaultMutableTreeNode3); 39 //获取某一节点到顶级节点(根节点)之间所有的节点 40 41 //获取某节点所对应的阶层 42 int level = defaultMutableTreeNode3.getLevel(); 43 44 //刷新重新绘制节点数(重要) 45 defaultTreeModel.reload(); 46 //获取所有节点:迭代器 47 Enumeration<DefaultMutableTreeNode> nodes = defaultMutableTreeNode.children(); 48 //判断某节点是否是根节点、页节点isLeaf() 49 ((DefaultMutableTreeNode)defaultMutableTreeNode.getChildAt(0)).isRoot(); 50 51 /*******************************************监听器****************************************/ 52 // 53 /***************JTree的监听器**************/ 54 //绑定选项监听器 55 //某节点被选中了 56 jTree2.addTreeSelectionListener(new TreeSelectionListener() 57 { 58 @Override 59 public void valueChanged(TreeSelectionEvent e) 60 { 61 // TODO Auto-generated method stub 62 // System.out.println("节点被选择"); 63 //获取选中的treePath,treePath即根节点至所选节点的集合 64 // System.out.println(tree.getSelectionPath()); 65 // //获取节点集合的数量 66 // System.out.println(tree.getSelectionPath().getPathCount()); 67 // //获取选中的节点对象 68 // System.out.println((DefaultMutableTreeNode)tree.getSelectionPath().getLastPathComponent()); 69 70 System.out.println(e.getPath().getLastPathComponent()); 71 } 72 }); 73 //为降低程序的错误率,监听器addTreeWillExpandListener和监听器addTreeExpansionListener通常会同时搭配使用, 74 //以确保程序菜单正常、正确打开 75 //绑定数结展开折叠的监听器 76 //节点被展开了 77 jTree2.addTreeExpansionListener(new TreeExpansionListener() 78 { 79 /** 80 * 展开事件 81 */ 82 @Override 83 public void treeExpanded(TreeExpansionEvent event) 84 { 85 // TODO Auto-generated method stub 86 System.out.println(event.getPath().getLastPathComponent()+" 被展开"); 87 } 88 /** 89 * 折叠 90 */ 91 @Override 92 public void treeCollapsed(TreeExpansionEvent event) 93 { 94 // TODO Auto-generated method stub 95 System.out.println(event.getPath().getLastPathComponent()+" 被折叠"); 96 } 97 }); 98 99 //绑定树结构将要执行展开和折叠的监听器 100 //节点将被展开:通常使用本方法对节点进行动态的显示控制 101 jTree2.addTreeWillExpandListener(new TreeWillExpandListener() 102 { 103 /** 104 * 将被展开的事件 105 */ 106 @Override 107 public void treeWillExpand(TreeExpansionEvent event) 108 throws ExpandVetoException 109 { 110 // TODO Auto-generated method stub 111 System.out.println(event.getPath().getLastPathComponent()+" 将被展开"); 112 113 } 114 /** 115 * 将被折叠的事件 116 */ 117 @Override 118 public void treeWillCollapse(TreeExpansionEvent event) 119 throws ExpandVetoException 120 { 121 // TODO Auto-generated method stub 122 System.out.println(event.getPath().getLastPathComponent()+" 将被折叠"); 123 } 124 }); 125 } 126 public static void main(String[] args) 127 { 128 JTreeKnow jTreeKnow = new JTreeKnow(); 129 jTreeKnow.setVisible(true); 130 } 131 }