Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8057791

Selection in JList is drawn with wrong colors in Nimbus L&F

    Details

    • Subcomponent:
    • Introduced In Version:
    • Resolved In Build:
      b127
    • CPU:
      x86
    • OS:
      generic

      Backports

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

          Issue Links

            Activity

            Hide
            yan Yuri Nesterenko (Inactive) added a comment - - edited
            RULE Swing_AllComponents/Automated/LAF_OrientationSuite/JListMultiSelItemTest any
            RULE Swing_AllComponents/Automated/LAF_OrientationSuite/JListSelItemTest any
            Show
            yan Yuri Nesterenko (Inactive) added a comment - - edited RULE Swing_AllComponents/Automated/LAF_OrientationSuite/JListMultiSelItemTest any RULE Swing_AllComponents/Automated/LAF_OrientationSuite/JListSelItemTest any
            Hide
            alexsch Alexander Scherbatiy (Inactive) added a comment -
            This is the regression from the fix JDK-8041725 Nimbus JList selection colors persist across L&F changes
            Show
            alexsch Alexander Scherbatiy (Inactive) added a comment - This is the regression from the fix JDK-8041725 Nimbus JList selection colors persist across L&F changes
            Hide
            alitvinov Anton Litvinov added a comment -
            The bug was reproduced with JDK 9 b118, JDK 8u92 b14, JDK 7u101 b14.
            Show
            alitvinov Anton Litvinov added a comment - The bug was reproduced with JDK 9 b118, JDK 8u92 b14, JDK 7u101 b14.
            Hide
            alitvinov Anton Litvinov added a comment -
            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.
            Show
            alitvinov Anton Litvinov added a comment - 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.
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/client/jdk/rev/51244f65475e
            User: alitvinov
            Date: 2016-06-23 09:55:23 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/client/jdk/rev/51244f65475e User: alitvinov Date: 2016-06-23 09:55:23 +0000
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/51244f65475e
            User: amurillo
            Date: 2016-07-13 03:50:56 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/51244f65475e User: amurillo Date: 2016-07-13 03:50:56 +0000

              People

              • Assignee:
                alitvinov Anton Litvinov
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: