Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Font color for disabled components  RSS feed

 
John Cozens
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to change the font color of the text within a disabled JButton, JCheckBox and non-editable JCheckBox.

For subclasses of JTextComponent (which none of these are), I appreciate that I could use:


I don't want to use, for example:

because this will change the disabled foreground of ALL disabled JComboBoxes within my app, which is undesirable - I want to be able to change the font color for selected comboboxes only.

I would also prefer not to have to play with UI Delegates, mainly because the 3rd party UI API I'm using has declared these final and I'm not in a position to amend the source code.

There are several previous postings about this issue on JavaRanch, and indeed in the Java Bug Parade, but none of them has offered a solution which works for my specific scenario.

Thanks in advance for your help and ideas.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want the font color to be the same as if enabled, just wrap the text
in <html></html> tags

eg
JButton btn = new JButton("<html>OK</html>");

if some other color, when disabled, include the font tag
btn.setText("<html><font color = blue>OK</font></html>");
 
John Cozens
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your fast response, Michael.

That works well for JButton, and should be fine for JComboBox too. I can probably live without JCheckBox foreground changing colour...it's only the tick I need to change, not any accompanying text label.

John
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the tick color is a whole lot more code

what you have to do is rip the static inner class 'CheckBoxIcon' out of
MetalIconFactory, modify for color, then set it as the icon for the checkbox
 
John Cozens
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael, understood. My problem here is that we're using a third party Look & Feel API which overrides the default UIxxx classes, and these all appear to have been marked final (and my company isn't happy to pay for a source code license). Not important enough to spend more time on right now, but I really appreciate your suggestion.

John
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!