JFrame 中的 JTree 颜色不变

JTree color not changing in JFrame

我已经为我的应用程序创建了一个 JTree。现在我想改变一些节点或整个树本身的颜色。我搜索并发现创建自定义 TreeCellRendererComponent 并更新其中的颜色,但它对我不起作用。也许我遗漏了某些东西,或者我在想也许我正在更新 JTree 的某些 属性,这导致颜色没有改变。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;

public class TestClass2 extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestClass2 frame = new TestClass2();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestClass2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout());
        contentPane.add(prepareCommandTree(), BorderLayout.CENTER);
    }


    public static JTree prepareCommandTree()
    {
        // Root node name which is the name of the command e.g. Display Text
        DefaultMutableTreeNode commandNode = new DefaultMutableTreeNode("Command Name");
        DefaultMutableTreeNode completeData = new DefaultMutableTreeNode("Complete Data");

        // Adding all branches under root branch
        commandNode.add(completeData);

        //create the tree by passing in the root node
        JTree commandTree = new JTree(commandNode);
        DefaultTreeModel model = (DefaultTreeModel)commandTree.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
        model.reload(root);

        // Setting JTree background
        commandTree.setOpaque(false);
        commandTree.collapseRow(0);
        commandTree.setBorder(new EmptyBorder(5, 0, 0, 0));
        commandTree.setFont( new Font(Font.MONOSPACED, Font.PLAIN, 13));

        // Adding image icon to the tree
        commandTree.setCellRenderer(new DefaultTreeCellRenderer(){
        private static final long serialVersionUID = 1L;

        public Component getTreeCellRendererComponent(final JTree tree,Object value,
          boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus)
        {
            // Trying to change color of tree
            setForeground(Color.RED);
            JLabel label = (JLabel)super.getTreeCellRendererComponent(tree,value,
                                                  sel,expanded,leaf,row,hasFocus);
                return label;
        }
        });

        // Setting adjustments to JTree properties
        commandTree.putClientProperty("JTree.lineStyle", "None");
        commandTree.setAlignmentX(Component.LEFT_ALIGNMENT);
        DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) commandTree.getCellRenderer();
        renderer.setLeafIcon(null);
        renderer.setClosedIcon(null);
        renderer.setOpenIcon(null);

        return commandTree;
    }
}

任何 suggestion/correction 都会有所帮助。谢谢。 :-)

您的渲染器设置其前景 Color 执行 super 调用之前。

super 方法还设置了一个前景 Color,它将替换您设置的前景。

只需在 super 调用之后应用 Color

    public Component getTreeCellRendererComponent(final JTree tree, final Object value,
            final boolean sel, final boolean expanded, final boolean leaf, final int row,
            final boolean hasFocus) {
        // Trying to change color of tree

        JLabel label = (JLabel) super.getTreeCellRendererComponent(tree, value,
                sel, expanded, leaf, row, hasFocus);
        setForeground(Color.RED);
        return label;// Or "return this", since the method actually returns the renderer component itself
    }
});