JDK-8057791 : Selection in JList is drawn with wrong colors in Nimbus L&F
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 8u20,9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: x86
  • Submitted: 2014-09-07
  • Updated: 2016-10-13
  • Resolved: 2016-06-23
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u112Fixed 9 b127Fixed
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Originally reproduced on MS Windows 8 OS, but should be generic for all supported OSes and not specific to only MS Windows OS.

A DESCRIPTION OF THE PROBLEM :
Wrong colors are used for selection foreground (black instead of white) and selection background (light gray instead of dark blue) in JList, when Nimbus L&F is set.
http://stackoverflow.com/questions/25685269/wrong-colors-in-jlist-when-using-nimbus-and-java-8u20

REGRESSION :
Last worked in JDK 8u11.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile, run the provided sample application and select any entry in JList.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The default colors should be used for selected entries in JList.
ACTUAL -
Since JDK 8u20 wrong colors are used.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

public class Test {
    public Test() {
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());

            JList<String> l = new JList<>();
            DefaultListModel<String> model = new DefaultListModel<>();
            model.add(0, "sssssssss");
            model.add(1, "sssssssss");
            model.add(2, "sssssssss");
            model.add(3, "sssssssss");
            l.setModel(model);

            JFrame f = new JFrame();
            f.setSize(500, 500);
            f.setLocationRelativeTo(null);
            f.add(l);
            f.pack();
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
final NimbusLookAndFeel laf = new NimbusLookAndFeel();
UIManager.setLookAndFeel(laf);
UIDefaults defaults = laf.getDefaults();
defaults.put("List[Selected].textForeground",
    laf.getDerivedColor("nimbusLightBackground", 0.0f, 0.0f, 0.0f, 0, false));
defaults.put("List[Selected].textBackground",
    laf.getDerivedColor("nimbusSelectionBackground", 0.0f, 0.0f, 0.0f, 0, false));
defaults.put("List[Disabled+Selected].textBackground",
    laf.getDerivedColor("nimbusSelectionBackground", 0.0f, 0.0f, 0.0f, 0, false));
defaults.put("List[Disabled].textForeground",
    laf.getDerivedColor("nimbusDisabledText", 0.0f, 0.0f, 0.0f, 0, false));
defaults.put("List:\"List.cellRenderer\"[Disabled].background",
    laf.getDerivedColor("nimbusSelectionBackground", 0.0f, 0.0f, 0.0f, 0, false));
Comments
It was practically proven that this bug is caused by the fix for JDK-8041725, since reversion of this fix from the latest version of JDK 9 development source code allows to eliminate this bug. It was defined that the bug is a result of fulfillment of the condition in "if" statement specified below in the method "javax.swing.plaf.synth.SynthStyle.getColor(SynthContext, ColorType)", which became possible after the fix for JDK-8041725, because the instance variables "JList.selectionForeground", "JList.selectionBackground" set via the corresponding UI delegate became instances of "javax.swing.plaf.nimbus.DerivedColor.UIResource" class. The file "jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthStyle.java": 800 if (color == null || color instanceof UIResource) { 801 // Then use what we've locally defined 802 color = getColorForState(context, type); 803 } When the method "javax.swing.plaf.synth.SynthStyle.getColor" is called and "javax.swing.plaf.synth.SynthListUI.SynthListCellRenderer" instance is set as a component in the method argument "SynthContext context", then though this "SynthListUI.SynthListCellRenderer" instance contains proper values of foreground, background colors, it can be only in only 1 state "SynthConstants.ENABLED" in run time and cannot be in "SynthConstants.SELECTED" state, therefore the call 802 color = getColorForState(context, type); always returns some default L&F foreground, background colors which are observed in this bug and which do not correspond to the correct color of JList in "SynthConstants.SELECTED" state.
07-06-2016

The bug was reproduced with JDK 9 b118, JDK 8u92 b14, JDK 7u101 b14.
23-05-2016

This is the regression from the fix JDK-8041725 Nimbus JList selection colors persist across L&F changes
29-10-2014

RULE Swing_AllComponents/Automated/LAF_OrientationSuite/JListMultiSelItemTest any RULE Swing_AllComponents/Automated/LAF_OrientationSuite/JListSelItemTest any
25-09-2014