I have the following really simple code for creating a JLabel and adding it to a panel using a MigLayout layout manager:
MigLayout layout = new MigLayout();
JPanel panel = new JPanel(layout);
panel.setBackground(Color.GRAY);
JLabel label = new JLabel("<html><h3>Some Text</h3></html>");
panel.add(label, "growx,w 220!,h 40!,top");
This should render the label as transparent so that the panel background is visible behind the label – this is the behaviour we want.
However on some PCs the label is rendered with a solid white background – ie as if opaque=true has been set.
Curiously, this isn’t a problem if the label text is not formatted using html.
JLabel label = new JLabel("Some text");
We are using BasicLabelUI as the UI delegate so I would expect to see standard painting behaviour here.
This is running using jre 1.6.0_30 in Windows XP. Has anyone seen something like this before? Could it be something to do with differences between the graphics hardware running on the various machines?
I’ve worked out what was happening after taking some time away from the problem.
It’s to do with Stylesheets and HTMLEditorKit.
Assume the following sample css:
The following code simple loads this into an new HTMLEditorKit instance, then adds a non-opaque html label to a panel.
}
Once imported, the stylesheet is made available to subsequent instances of HTMLEditorKit, such as the one being used to render the label. So in the code sample, the non-opaque label is shown with a yellow background.
If you create the label before installing the styles in the example, the label (and subsequent labels) will not pick up the style and be rendered non-opaque as desired.
The problem we have is that our application can be launched both separately and from within another app (running in the same jre). Some users of the second app have access to a screen that installs a stylesheet before our app can be launched. In these instances the styles are loaded and stored as defaults ready to be used by our app. This is why some users reported a problem and others didn’t.
I haven’t decided on the best solution for this yet. One possibility was to override the background style when creating the label html:
However, if you use ‘transparent’ instead of a solid colour, the underlying colour from the stylesheet is shown instead. Annoying!