整理swing之JTree渲染(1)

/**
 * 树控件使用
 * @author lgh
 */
public class JTreeTest extends JFrame {

    public JTreeTest() {
        init();
    }

    private void init() {
        this.setSize(800, 600);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.add(createJTree(), BorderLayout.CENTER);
        this.setVisible(true);
    }

    /**
     * 创建一个树
     * @return
     */
    private JScrollPane createJTree() {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode();
        node.setUserObject(new MyTreeNode(new ImageIcon(Pic.tree2), "root"));
        JTree jt = new JTree(node);
        jt.setCellRenderer(new MyTreeCellRenderer());
        createRootMutableTreeNode(node);
        JScrollPane jsp = new JScrollPane(jt);
        return jsp;

    }

    private void createRootMutableTreeNode(DefaultMutableTreeNode root) {
        root.add(new DefaultMutableTreeNode(new MyTreeNode(new ImageIcon(Pic.tree1), "node1")));
        root.add(new DefaultMutableTreeNode(new MyTreeNode(new ImageIcon(Pic.tree1), "node2")));
        root.add(new DefaultMutableTreeNode(new MyTreeNode(new ImageIcon(Pic.tree1), "node3")));
        root.add(new DefaultMutableTreeNode(new MyTreeNode(new ImageIcon(Pic.tree1), "node4")));
        root.add(new DefaultMutableTreeNode(new MyTreeNode(new ImageIcon(Pic.tree1), "node5")));
    }

    public static void main(String[] args) {
        JTreeTest jTreeTest = new JTreeTest();
    }

    private class MyTreeCellRenderer extends DefaultTreeCellRenderer {

        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            MyTreeNode jtreeNode = (MyTreeNode) node.getUserObject();

            this.setIcon(jtreeNode.getIcon());
            this.setText(jtreeNode.getName());
            this.setOpaque(true);
            if (selected) {
                this.setBackground(new Color(178, 180, 191));
            } else {
                this.setBackground(new Color(255, 255, 255));
            }
            return this;
        }
    }

    private class MyTreeNode extends DefaultMutableTreeNode {

        private Icon icon;
        private String name;

        public MyTreeNode(Icon icon, String name) {
            this.icon = icon;
            this.name = name;
        }

        public MyTreeNode() {
            super();
        }

        /**
         * @return the icon
         */
        public Icon getIcon() {
            return icon;
        }

        /**
         * @param icon the icon to set
         */
        public void setIcon(Icon icon) {
            this.icon = icon;
        }

        /**
         * @return the name
         */
        public String getName() {
            return name;
        }

        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值