I have a jButton with HTML embedded in the label text, and when the
jButton is disabled, the label is not greyed-out in the usual way. How
do I get around this? Below is a code snippet illustrating the
behaviour:
import java.awt.*;
import javax.swing.*;
public class Test{
JFrame f = new JFrame();
JButton jButton1 = new JButton(
"<html><p align=\"center\">Button with HTML<br>in the
caption</p></html>");
JButton jButton2 = new JButton("Another button without any HTML");
public Test(){}
public void launch(){
jButton1.setEnabled(false);
jButton2.setEnabled(false);
f.getContentPane().setLayout(new GridBagLayout());
f.getContentPane().add(jButton1, new GridBagConstraints(
0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
f.getContentPane().add(jButton2, new GridBagConstraints(
0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args){
Test t = new Test();
t.launch();
}
}
Mike.
Filip Larsen - 30 Jun 2005 09:08 GMT
Mike wrote
> I have a jButton with HTML embedded in the label text, and when the
> jButton is disabled, the label is not greyed-out in the usual way. How
> do I get around this?
To my knowledge, the enabled state is not carried over to the ultimate
component responsible to render the html text (see the source code for
javax.swing.plaf.basic.BasicButtonUI and
javax.swing.plaf.basic.BasicHTML for details here).
You can simulate the effect of disabling by setting the foreground color
just like the BasicButtonUI do on non-HTML text:
jButton1.setEnabled(false);
jButton1.setForeground(jButton1.getBackground().darker());
and set it back to its previous color when you re-enable the button. If
you have many buttons like this you can write a small class that listens
on the button and do it all for you.
Regards,

Signature
Filip Larsen