tags:

views:

259

answers:

2

In Swing, is there a property to set a JList disabled foreground color?

I'm using the Netbeans GUI builder and I want to add a property to the resource properties file that sets the color of the text in a JList to a different default when the JList is disabled. Using the Nimbus LAF, there is a different color for this because the text is gray when disabled but black when enabled. I just don't want it to be grey when disabled.

The standard foreground color is just .foreground.

Thanks.

+1  A: 

If there is a property to set then the UIManager Defaults program should show you the property to change. I don't see a property for the Metal or Windows LAF so it may indicate this is controlled directly in the UI code.

camickr
+3  A: 

The DefaultListCellRenderer extends JLabel. In its getListRendererComponent method, it sets its enabled state based on that of the JList that is passed in.

The code for painting disabled JLabel text in BasicLabelUI does some work to paint the text with a shadow effect. In many subclasses, you will find code that looks for the "Label.disabledForeground" UI property. The Nimbus defaults seem to look for "Label.disabledText".

You have a couple options:

  1. You can set the "Label.disabledText" property in the UIManager, which will make all JLabel instances and subclasses that are disabled to take on this coloring.
  2. You can create a custom renderer for your JList that tests the enabled state of the JList and then does whatever custom code that you would like - or omit the enabled state test entirely if you want it to look the same regardless of its enabled state.

I would suggest that you take the custom renderer approach, as it is difficult to say where the change of a JLabel property will show up, as that class is used as a builing block in many different components.

akf
Thanks for the tip. I forgot about renderers. Ended up extending DefaultListCellRenderer, overriding getListRendererComponent, calling super getListRendererComponent and before returning, calling setEnabled(true). That does exactly what I really wanted.
Jay R.