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

孤街醉人 提交于 2019-12-13 15:56:48

问题


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:

  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.




回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!