JDK-7150926 : Color differs for disabled buttons when text is html formatted
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_7
  • CPU: x86
  • Submitted: 2012-03-04
  • Updated: 2012-03-28
  • Resolved: 2012-03-07
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b05)
Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Windows 7

A DESCRIPTION OF THE PROBLEM :
The problem has already been described in bug 4783068.
But the workarounds (see code) don't work any more.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run and click on the "enabled" checkbox.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Text color of disabled components should always look the same, no matter whether the text is html formatted or not.

ACTUAL -
Color differs considerably and text is hardly readable when html formatted and component is disabled.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.*;

public class HTMLTextColour extends JFrame implements ActionListener {
  JButton bRegular, bHTML;
  JCheckBox check;

  public HTMLTextColour() {
    setSize(250, 110);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    bRegular= new JButton("Sample text");
    add(bRegular);
    bHTML= new JButton("<html><font color=\"#333333\">HTML text<html>") {
      public void setEnabled(boolean b) {
	super.setEnabled(b);
//	setForeground(b ? UIManager.getColor("Button.foreground") :
//			  UIManager.getColor("Button.disabledText"));
//	setForeground(b ? UIManager.getColor("Label.foreground") :
//			  UIManager.getColor("Label.disabledForeground"));
//	setForeground(b ? UIManager.getColor("Label.foreground") :
//			  UIManager.getColor(new ColorUIResource(153,153,153)));
	setText(b ? "<html><font color=\"#333333\">HTML text<html>" :
			"<html><font color=red>Should be red<html>");
//	repaint();
      }
    };
    add(bHTML);
    check= new JCheckBox("Enabled", true);
    check.addActionListener(this);
    add(check);
    setVisible(true);
  }

  public static void main(String argsv[]) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
	new HTMLTextColour();
      }
    });
  }


  public void actionPerformed(ActionEvent evt) {
    bRegular.setEnabled(check.isSelected());
    bHTML.setEnabled(check.isSelected());
DBG.p(bRegular.getForeground());
DBG.p(bHTML.getForeground());
  }

}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Overriding "setEnabled(boolean b)" used to be a workaround, but isn't any more.

Comments
WORK AROUND Use another LAF, or another Theme for MetalLAF, or set own "textInactiveText" property in UIManager.
07-03-2012

EVALUATION For disabled HTML text the UIManager.getColor("textInactiveText")) color is used, but for buttons the UIManager.getColor("Button.disabledText") is used. We can't use for buttons "textInactiveText" because of backward compatibility, and we can't use "Button.disabledText" for disabled HTML text, because HTML text is used in Labels and other components. In MetalLAF for "textInactiveText" color is used theme.getInactiveSystemTextColor(), so you can either use another Theme, or set own "textInactiveText" property in UIManager.
07-03-2012