JTree Nimbus Selectionbackground 不适用于 Java 1.8

JTree Nimbus Selectionbackground not Working on Java 1.8

我已经按照 this approach 修改了 JTree SelectionBackground。它在 Java 1 中工作正常。7.x 而选择是白色的是 Java 1.8.Ref截图。有什么办法可以解决吗

重现此问题的 MCVE 代码

import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIDefaults;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;


public class Test {

public static void main(String[] args) {
    setNimbusLookAndFeel();
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            treeTest();
        }
    });
}

private static void setNimbusLookAndFeel() {
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
}

public static void treeTest() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTree tree = new JTree();
    setLAF(tree);
    setCellRenderer(tree);
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    for (int i = 0; i < 10; i++) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode("Parent " + i);
        for (int j = 0; j < 10; j++) {
            node.add(new DefaultMutableTreeNode("Child " + j));
        }
        root.add(node);
    }
    tree.setModel(new DefaultTreeModel(root));
    tree.setSelectionPath(new TreePath(root.getPath()));
    frame.add(new JScrollPane(tree));
    frame.pack();
    frame.setVisible(true);
}

private static void setLAF(JTree tree) {
    UIDefaults paneDefaults = new UIDefaults();
    paneDefaults.put("Tree.selectionBackground", null);
    tree.putClientProperty("Nimbus.Overrides", paneDefaults);
    tree.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
    tree.setBackground(Color.WHITE);
}

private static void setCellRenderer(JTree tree) {
    tree.setCellRenderer(new DefaultTreeCellRenderer() {
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean isLeaf, int row, boolean focused) {
            Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, isLeaf, row, focused);
            setBackgroundSelectionColor(Color.BLUE);
            return c;
        }

    });
}
}

您可以使用 setBackground(...) 而不是 setBackgroundSelectionColor(...):

tree.setCellRenderer(new DefaultTreeCellRenderer() {
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean isLeaf, int row, boolean focused) {
        JComponent c = (JComponent) super.getTreeCellRendererComponent(tree, value, selected, expanded, isLeaf, row, focused);
        //setBackgroundSelectionColor(Color.BLUE);
        c.setBackground(selected ? Color.BLUE : tree.getBackground());
        c.setOpaque(true);
        return c;
    }
});