简体   繁体   English

自定义 JTree 渲染器 - 支持 CheckBoxes 和 Default

[英]Custom JTree Renderer - Support both CheckBoxes and Default

I am trying to create a Custom JTree Renderer that can display both JChckBoxes and JLabel with Colored Text.我正在尝试创建一个自定义 JTree 渲染器,它可以显示带有彩色文本的 JChckBoxes 和 JLabel。

When I create a JTree with that renderer, it shows all nodes garbled.当我使用该渲染器创建 JTree 时,它显示所有节点都是乱码。 在此处输入图像描述

Below is my code.下面是我的代码。 Not sure what I am doing wrong.不知道我做错了什么。 If I remove Colored text and normal text and only keep JCheckbox or Only use colored text, it works fine.如果我删除彩色文本和普通文本,只保留 JCheckbox 或只使用彩色文本,它工作正常。

Appreciate any help.感谢任何帮助。

My Renderer class:我的渲染器 class:

public class CheckBoxCellRenderer extends JPanel implements TreeCellRenderer {

    public CheckBoxCellRenderer() {
        super();
        this.setLayout(new BorderLayout());
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean selected, boolean expanded, boolean leaf, int row,
            boolean hasFocus) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
        Object obj = node.getUserObject();
        if(obj instanceof JCheckBox){
            JCheckBox checkBox = (JCheckBox) obj;
            //TreePath tp = new TreePath(node.getPath());
            if(hasFocus)
            {
                checkBox.setBackground(Color.darkGray);
                checkBox.setForeground(Color.white);
            } else {
                checkBox.setBackground(Color.white);
                checkBox.setForeground(Color.black);
            }
            add(checkBox, BorderLayout.CENTER);
            setOpaque(false);
            this.validate();
            System.out.println("JCheckBox");
            return this;
        } else if(obj instanceof ColoredString1){
            JLabel label = new JLabel();
            ColoredString1 cString = (ColoredString1) obj;
            if(hasFocus)
            {
                label.setBackground(Color.gray);
                label.setForeground(cString.getColor());
            } else {
                label.setBackground(Color.white);
                label.setForeground(cString.getColor());
            }
            label.setText(cString.getValue());
            add(label, BorderLayout.CENTER);
            this.validate();
            System.out.println("ColoredString1");
            return this;
        } else {
            JLabel label = new JLabel();
            if(hasFocus)
            {
                label.setBackground(Color.gray);
            } else {
                label.setBackground(Color.white);
            }
            label.setText((String) obj);
            add(label, BorderLayout.CENTER);
            this.validate();
            System.out.println("Default");
            return this;
        }
    }
}

My main code sample:我的主要代码示例:

public class JPOC_JTree_Custom {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JFrame myframe = new JFrame("Custom JTree POC");
        myframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
        JTree myTree = new JTree();
        myTree.setCellRenderer(new CheckBoxCellRenderer());
        //((DefaultMutableTreeNode)((DefaultTreeModel)myTree.getModel()).getRoot()).removeAllChildren();
        DefaultTreeModel model = (DefaultTreeModel) myTree.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
        root.removeAllChildren();
        root.setUserObject("Root_Test");
        
        DefaultMutableTreeNode TreeNode_Ex_1 = new DefaultMutableTreeNode(new ColoredString1("Red", Color.red));
        DefaultMutableTreeNode TreeNode_Ex_2 = new DefaultMutableTreeNode(new ColoredString1("Orange", Color.orange));
        DefaultMutableTreeNode TreeNode_Ex_3 = new DefaultMutableTreeNode(new ColoredString1("Green", Color.green));
        DefaultMutableTreeNode TreeNode_Ex_4 = new DefaultMutableTreeNode(new ColoredString1("Blue", Color.blue));
        DefaultMutableTreeNode TreeNode_Ex_5 = new DefaultMutableTreeNode("5");
        DefaultMutableTreeNode TreeNode_Ex_6 = new DefaultMutableTreeNode(new JCheckBox("Check?"));
        
        root.add(TreeNode_Ex_1);
        root.add(TreeNode_Ex_2);
        root.add(TreeNode_Ex_3);
        root.add(TreeNode_Ex_4);
        root.add(TreeNode_Ex_5);
        root.add(TreeNode_Ex_6);
        
        //model.setRoot(root);
        model.reload(root);
        //myTree.setModel(model);
        
        JScrollPane myTree_ScrollPane = new JScrollPane();
        myTree_ScrollPane.setViewportView(myTree);
        
        myframe.add(myTree_ScrollPane);
        
        myframe.validate();
        
        myframe.setVisible(true);
        
        System.out.println("Done");
    }
    
}

-- --

As @VGR confirmed, after adding removeAll before add resolved this issue for me.正如@VGR 确认的那样,在添加 removeAll 之前添加为我解决了这个问题。

removeAll();
add(label, BorderLayout.CENTER);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM