Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8640641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:19:46+00:00 2026-06-12T11:19:46+00:00

I have the following really simple code for creating a JLabel and adding it

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:19:48+00:00Added an answer on June 12, 2026 at 11:19 am

    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:

    body  {
    background-color: #ffff00}
    

    The following code simple loads this into an new HTMLEditorKit instance, then adds a non-opaque html label to a panel.

    public class HTMLLabelTest extends JFrame {
    
    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new HTMLLabelTest();
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
    
    public HTMLLabelTest() {
    
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    
        HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
        StyleSheet styleSheet = new StyleSheet();
        URL resource = getClass().getResource("/stylesheet.css");
        styleSheet.importStyleSheet(resource);
        htmlEditorKit.setStyleSheet(styleSheet);
    
        JLabel label = new JLabel();
        label.setText("<html><B>Some HTML Formatted</B> text</html>");
        label.setOpaque(false);
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setHorizontalTextPosition(JLabel.CENTER);
        label.setPreferredSize(new Dimension(30,20));
    
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(label);
    
        panel.setOpaque(true);
        panel.setBackground(Color.WHITE);
    
        add(panel);
        setPreferredSize(new Dimension(300,200));
    }
    

    }

    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:

    <html><body bgcolor=\"red\">Test</body></html>
    

    However, if you use ‘transparent’ instead of a solid colour, the underlying colour from the stylesheet is shown instead. Annoying!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simple code for testing a webservice : using System; using
I have the following really simple code <asp:ScriptManager ID=ScriptManager1 runat=server> </asp:ScriptManager> <asp:UpdatePanel ID=UpdatePanel1 runat=server>
I have a really simple class, and within the implementation I have this code:
I'm sure this is a really simple question. The following code shows what I'm
I'm writing a really simple IoC/DI container, and I've got the following code: ConstructorInfo[]
Hi I am using the Simple Injector DI library and have been following some
I have had the following problem for a while and I am really not
I have to really ask this question as I donot know Python. Following are
I really need advice on how to do the following. I have tried several
I have a question, although I can't really go into specifics. Will the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.