<Swing Dev> Setting font color of disabled button with HTML <font> tag.

Alexander Thaller alex.t at gmx.at
Mon Sep 3 11:32:03 UTC 2012


Hi.
I have a JButton with multiple lines of text where I can change the 
color of a part of the text by html tag <font> . I now wanted to change 
the font color of a disabled button with <font> tag too, but this only 
works with Java 6 but NOT with Java 7. See below for an example code 
that simply creates a JFrame and places 2 Buttons inside, one is enabled 
the other is disabled. Changing the text color of the disabled button 
with <font> is only possible with Java 6. See 
http://imgur.com/E48le,XRppa#0 for a screenshot of Java 6 and 
http://imgur.com/E48le,XRppa#1 for Java 7. It seems like the UIManger 
property for color of disabled text is overriding the HTML color 
information in Java 7 but not in Java 6.
Is this difference between the two versions wanted and how can I change 
the font color of part of the text of a disabled JButton with Java 7?
P.S. My OS is Windows 7 64 Bit and Java versions are 1.6.0_20 and 1.7.0_07

import javax.swing.*;
public class ButDisColorchange {
     public static void main(String[] args) {
         JFrame f=new JFrame();
         f.setSize(300,200);
         JButton bEnabled= new JButton("<html><font color=\"green\">This 
is an</font> <i>ENABLED</i> Button</html>");
         JButton bDisabled= new JButton("<html><font color=\"red\">This 
is an</font> <i>DISABLED</i> Button</html>");
         bDisabled.setEnabled(false);
         JPanel pane= new JPanel();
         pane.add(bEnabled);
         pane.add(bDisabled);
         f.add(pane);
         f.setVisible(true);
     }
}




More information about the swing-dev mailing list