JTree实例
private void createTreeByXdDdt() { DefaultComboBoxModel boxModel = (DefaultComboBoxModel) cmbXdDdt.getModel(); XdDdt xdDdt = (XdDdt) boxModel.getSelectedItem(); // 得到选中的调度台对象; DefaultMutableTreeNode root = new DefaultMutableTreeNode(xdDdt); List<PLthDes> pLthDesList = getPLthDesListByLthid(xdDdt.getLth()); // 根据调度台获得p_lth_des对象集合 for (PLthDes pLthDes : pLthDesList) { DefaultMutableTreeNode pLthDesNode = new DefaultMutableTreeNode(pLthDes); List<PThNode> pThNodes = getPThNodesByIds(pLthDes.getLthid(),pLthDes.getThid()); // 根据p_lth_des获得p_th_node对象集合 for (PThNode pThNode : pThNodes) { DefaultMutableTreeNode pThNodeTreeNode = new DefaultMutableTreeNode(pThNode); pLthDesNode.add(pThNodeTreeNode); } root.add(pLthDesNode); } DefaultTreeModel treeModel = new DefaultTreeModel(root); treeModel.addTreeModelListener(new TreeModelListener() { @Override public void treeStructureChanged(TreeModelEvent e) { } @Override public void treeNodesRemoved(TreeModelEvent e) { } @Override public void treeNodesInserted(TreeModelEvent e) { } @Override public void treeNodesChanged(TreeModelEvent e) { DefaultMutableTreeNode treeNode; TreePath treePath = tree.getSelectionPath(); if(treePath!=null){ treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); Object userObject = treeNode.getUserObject(); System.out.println("TreeModelListener->"+userObject.getClass()); }else{ System.out.println("TreeModelListener->"+"treePath is null"); } } }); tree.setModel(treeModel); // 设置树的model tree.setCellRenderer(new TreeRender()); // 设置树的渲染 tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); MouseListener[] mouseListeners = tree.getMouseListeners(); System.out.println("mouseListeners:"+mouseListeners.length); tree.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { DefaultMutableTreeNode treeNode; // TreePath treePath = tree.getPathForLocation(e.getX(), e.getY()); // 这样的话,第二次不选中就会是null TreePath treePath = tree.getSelectionPath(); // 这样的话,第二次不选中也会触发事件; if(treePath!=null){ treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); Object userObject = treeNode.getUserObject(); System.out.println("MouseClick->"+userObject.getClass()); if(userObject instanceof PLthDes){ PLthDes pLthDes = (PLthDes) userObject; System.out.println("MouseClick->"+pLthDes.getLthid()+":"+pLthDes.getThid()); } }else{ System.out.println("MouseClick->"+"treePath is null"); } } }); MouseListener[] mouseListeners2 = tree.getMouseListeners(); System.out.println("mouseListeners2:"+mouseListeners2.length); TreeSelectionListener[] listeners = tree.getTreeSelectionListeners(); System.out.println("TreeSelectionListener:"+listeners.length); tree.addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode treeNode; TreePath treePath = tree.getSelectionPath(); if(treePath!=null){ treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); Object userObject = treeNode.getUserObject(); System.out.println("TreeSelectionListener->"+userObject.getClass()); }else{ System.out.println("TreeSelectionListener->"+"treePath is null"); } } }); TreeSelectionListener[] listeners2 = tree.getTreeSelectionListeners(); System.out.println("TreeSelectionListener2:"+listeners2.length); }