A color object not working as a color unless recreated

145 views Asked by At

I'm struggling with a descriptive title.

I'm trying to use the swing.plaf colors from NimbusLookAndFeel in a custom component of mine. The colors i'm getting from UIManager looks good, but the setBackground and setForeground methods doesn't work with the colors im getting.

The following sample displays the very curious behaviour

package syscolorbug;

import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class SysColorBug {
    public static void main(String[] args) throws Exception {
        javax.swing.UIManager.
                setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");

        Color background = javax.swing.UIManager.getDefaults().
                getColor("List[Selected].textBackground"); 
        JFrame jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setLayout(new FlowLayout());
        jf.setSize(400,300);
        JLabel l = new JLabel("Text");
        l.setOpaque(true);
        jf.add(l);
        System.err.println(background);
        // This needs to happen for the color to be set.
        //24: background = new Color(background.getRGB());
        System.err.println(background);
        l.setBackground(background);
        jf.setVisible(true);
    }

}

If i comment out 24: it works as intended.

The debug output on background shows

DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138

and

java.awt.Color[r=57,g=105,b=138]

Further investigations shows that the problem doesn't seem to be affecting other LookAndFeels.

For instance on the Metal LAF produces

javax.swing.plaf.ColorUIResource[r=255,g=255,b=255]

for the 'List.background' key.

Is this a bug in the Nimbus plaf?

I'd very much like to avoid the superflous background = new Color(background.getRGB()); is there an easy fix?

Java version is openjdk java 1.8.0.

1

There are 1 answers

1
WJS On

I don't do laf much when I do graphics so I don't understand why List[Selected].textBackground would be the background of the JLabel. The default appears to be color=214,217,223

This also seems to match what I saw and read at nimbus laf