Wrong colors in JList when using Nimbus and Java 8U20

My application is using the wrong colors for the JList as I am updating to the latest Java 8 (U20). For example. instead of blue, light gray is actually used for the selected items.

Simple test application:

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

      

Java 7, Java 8

Java 7, Java 8 <U20

Java 8 U20:

Java 8 U20

JList.getSelectionBackground()

returns

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

      

but it's not actually RGB (57,105,138) but the aforementioned light gray.

+3


source to share


2 answers


You can restore the exact behavior of pre versions 1.8.0_20

with the following initialization code:

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

      



This returns what has changed between 1.8.0_05

and 1.8.0_20

in the class NimbusDefaults

. The parameter has false

been removed (which effectively translates it to true

with an overloaded method). This change turns Color

into UIResource

something that might be formally correct, but for whatever reason, it causes the problem you are facing. Therefore, reinstalling it false

restores the previous behavior.

+3


source


I had the same problem. This is how I solved it for my application:

 ...

 for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
  ...

  UIManager.getLookAndFeelDefaults().put("List[Selected].textBackground", new Color(57, 105, 138));
  UIManager.getLookAndFeelDefaults().put("List[Selected].textForeground", Color.WHITE);

      

Here is a list of UIManager keys (Color):

http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html



Hope this helps you,

Edit: Tested with the provided code. He works.

Regards

+2


source







All Articles