JTree Nimbus Selectionbackground不适用于Java 1.8

时间:2015-11-26 12:59:16

标签: java swing jtree nimbus treecellrenderer

我已经按照this approach修改了JTree SelectionBackground。它在Java 1.7.x中运行良好,而选择是白色的是Java 1.8.Ref截图。有没有办法解决它。

enter image description here

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;
        }

    });
}
}

1 个答案:

答案 0 :(得分:2)

您可以使用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;
    }
});