问题
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:
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:
- You can set the
"Label.disabledText"
property in theUIManager
, which will make allJLabel
instances and subclasses that are disabled to take on this coloring. - You can create a custom renderer for your
JList
that tests the enabled state of theJList
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.
回答2:
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.
来源:https://stackoverflow.com/questions/2276691/in-swing-is-there-a-property-to-set-a-jlist-disabled-foreground-color