JTree 常用方法
package com.swing.demo; import java.awt.Component; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.SwingUtilities; import javax.swing.event.TreeExpansionEvent; import javax.swing.event.TreeExpansionListener; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel; import com.swing.model.User; /** * JTree 常用方法. * @author Administrator * */ public class TreeDemo { public static void main(String[] args) { final JTree tree = createTree(); // 窗体设置 JFrame frame = new JFrame("JTreeDemo"); // 构造完树之后还要把树放在JScollPanel上,否则树显示会有问题 JScrollPane treeView = new JScrollPane(tree); frame.add(treeView); frame.setSize(300, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 添加树节点选择事件 tree.addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { // 取得TreePath // TreePath newPath = event.getNewLeadSelectionPath(); // TreePath oldPath = event.getOldLeadSelectionPath(); // 通过JTree取得Node // JTree tree = (JTree) event.getSource(); // tree.getLastSelectedPathComponent(); DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree .getLastSelectedPathComponent(); if (node == null) { return; } Object object = node.getUserObject(); if (node.isLeaf()) { User user = (User) object; System.out.println("你选择了:" + user.toString()); } // 取得根节点: TreeNode rootNode = (TreeNode) tree.getModel().getRoot(); // 取得树的数据模型: DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel(); // 取得树的某一个节点的位置: TreePath nodePath = new TreePath(treeModel.getPathToRoot(node)); // 设置节点的展开选择和可见性: tree.setSelectionPath(nodePath); tree.makeVisible(nodePath); // 展开后滚动至节点可见: tree.scrollPathToVisible(nodePath); // 还有一个是设置鼠标点击展开树节点的(默认是双击,改成了0就是不可点击): tree.setToggleClickCount(1); // 另外就是一个设置Cell的高度的,但这个方法我们一般不用,大多时候是在Renderer里设置的,因为这样设置可能有UI问题: // tree.setRowHeight(15); } }); // 树节点展开(折叠) 事件 tree.addTreeExpansionListener(new TreeExpansionListener() { @Override public void treeExpanded(TreeExpansionEvent event) { } @Override public void treeCollapsed(TreeExpansionEvent event) { } }); tree.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1) { // Returns the last path component DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree .getLastSelectedPathComponent(); } else if (SwingUtilities.isRightMouseButton(e)) { // Returns the row for the specified location. // int selectRow = tree.getRowForLocation(e.getX(), // e.getY()); // Returns the path for the node at the specified location. TreePath selectPath = tree.getPathForLocation(e.getX(), e.getY()); if (selectPath != null) { // set select tree.setSelectionPath(selectPath); // get tree node. DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) selectPath .getLastPathComponent(); } } } }); // 渲染器 tree.setCellRenderer(new DefaultTreeCellRenderer() { /** * serialVersionUID */ private static final long serialVersionUID = 1L; @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); } }); } /** * createTree * * @return */ private static JTree createTree() { // 创建没有父节点和子节点、但允许有子节点的树节点,并使用指定的用户对象对它进行初始化。 // public DefaultMutableTreeNode(Object userObject) DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("软件部"); node1.add(new DefaultMutableTreeNode(new User("小花"))); node1.add(new DefaultMutableTreeNode(new User("小虎"))); node1.add(new DefaultMutableTreeNode(new User("小龙"))); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("销售部"); node2.add(new DefaultMutableTreeNode(new User("小叶"))); node2.add(new DefaultMutableTreeNode(new User("小雯"))); node2.add(new DefaultMutableTreeNode(new User("小夏"))); DefaultMutableTreeNode top = new DefaultMutableTreeNode("职员管理"); top.add(new DefaultMutableTreeNode(new User("总经理"))); top.add(node1); top.add(node2); // JTree(TreeNode root) // 返回 JTree,指定的 TreeNode 作为其根,它显示根节点。 JTree tree = new JTree(top); // 最终JTree也是通过setModel设置数据的: // tree = new JTree(); // tree.setModel(treeModel); tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION); return tree; } private List<TreeNode> treeNodeList = new ArrayList<TreeNode>(); /** * 从任何节点递归其下所有的例子. * * @param treeNode */ public void getChildNodeList(TreeNode treeNode) { if (treeNode == null) { throw new RuntimeException("error treenode."); } if (treeNodeList == null) { treeNodeList = new ArrayList<TreeNode>(); } if (treeNode.getChildCount() >= 0) { for (Enumeration<TreeNode> e = treeNode.children(); e .hasMoreElements();) { TreeNode childNode = (TreeNode) e.nextElement(); treeNodeList.add(childNode); getChildNodeList(childNode); } } } }