Take the following example code. There is some bug in Swing which doesn't render disabled components as disabled if the component contains HTML. Aside from reporting the issue, which I hope a colleague has already taken care of, is there some good way to work around the problem?
Whatever solution I take, I want it to be a global fix as opposed to something that needs to be hacked into every check box in the application.
I tried making a custom UI for the check box which calls setForeground
before and after the painting, but it turns out that by calling setForeground
, it fires an event which ultimately results in it calling repaint()
, which calls the renderer, ...
import java.awt.GridLayout;
import java.util.Arrays;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class TestCheckBoxes extends JFrame
{
public TestCheckBoxes()
{
JCheckBox checkBox1 = new JCheckBox("Enabled, plain text");
JCheckBox checkBox2 = new JCheckBox("<html><p>Enabled, HTML");
JCheckBox checkBox3 = new JCheckBox("Disabled, plain text");
checkBox3.setEnabled(false);
JCheckBox checkBox4 = new JCheckBox("<html><p>Disabled, HTML");
checkBox4.setEnabled(false);
setLayout(new GridLayout(4, 1));
for (JCheckBox checkBox : Arrays.asList(checkBox1, checkBox2, checkBox3, checkBox4))
{
checkBox.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
add(checkBox);
}
((JComponent) getContentPane()).setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
pack();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
TestCheckBoxes frame = new TestCheckBoxes();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}