Morinar Morinar - 4 months ago 6
Java Question

Why is a JLabel in a JPopupMenu not respecting its Look and Feel?

I have a Java application with a custom defined Look and Feel in which the user can switch between a light and a dark mode. One of the things we have is a right click popup menu that inherits from the JPopupMenu class. The first thing we add to the menu is a JLabel:

add( new JLabel( "<html><i> " + field.desc + "</i><br/>field = " + field.getName() + "</html>" ) );

Now, we have altered the L&F for JLabels and they appear in quite a few places in the app styles correctly. We have modified:


Additionally, the L&F for the MenuItems has also been modified via:


For what it's worth, we've also modified similar properties on MenuItems.

The problem is that that label that occurs inside the popup menu doesn't seem to be respecting the background color. The foreground color changes when switching between light and dark mode, but the background color does not. I did a screen grab and eyedropped the color that the background color was set to and couldn't find a match anywhere in our L&F settings, which would seem to suggest that it was just using the default L&F (Windows probably) on it.

Is this perhaps a bug? Or am I not setting some L&F property I should be? I couldn't seem to find anybody in the Google-sphere with the same problem, so any help would be appreciated.


I should have known that this would be the answer: the JLabel was set to transparent so it's background wasn't being respected. Altering the code like this fixed the problem real nice:

JLabel fieldInfo = new JLabel( "<html><i> " + field.desc + "</i><br/>field = " + field.getName() + "</html>" );
fieldInfo.setOpaque( true );