How to use default Nimbus color with UIManager?

I have a custom ListCellRenderer and would like to use the default background color of Nimbus. I can find the color with:

Color selectionBackground = UIManager.getColor("nimbusSelectionBackground");

and if I print it, it will have the same values as the default colors of Nimbus . But when I use it on JPanel, I get a different gray color, how can I use the color from UIManager?

When I do this:

setBackground(Color.RED);

JPanels backlight is shown in red, but when I do:

setBackground(selectionBackground);

The color "selectionBackground" is not used , but gray.


Here is an example and a screenshot:

enter image description here

The background should be:

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class PanelColor {

    public static void main(String[] args) {

        // switch to Nimbus Look And Feel
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                try {
                    UIManager.setLookAndFeel(info.getClassName());
                } catch (Exception e) { e.printStackTrace(); }
                break;
            }
        }

        Color selectionBackground = UIManager.getColor("nimbusSelectionBackground");

        JPanel panel = new JPanel(new BorderLayout());
        panel.setPreferredSize(new Dimension(300,50));
        panel.add(new JLabel(selectionBackground.toString()), BorderLayout.NORTH);

        // is not showing the selectionBackground color
        panel.setBackground(selectionBackground);

        JFrame frame = new JFrame();
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
+5
source share
2

, -, , . , Color ( ) . , , L & F .

, DerivedColor UIManager. .

L & Fs . . GTK L & F , , . , , Swing () (b) L & F , , , .

+4

, "", . , , UIManager.getColor() ColorUIResource.

ColorUIResource - , UIResource. Javadoc, L & Fs " , , ". Nimbus , , , , .

+5

All Articles