TL;DR
Is there a way to getting rid of the icon gap between the MenuItem border and the defined text? It appears as soon as I create the menu item, even if I do not set an icon, as well as if I set the icon to null.
Long Version
The Java application we develop in our company exists since the 90'. In the 2000's it got restyled and used to have one of those awful, eehrm I mean fancy and colourful, "Synthetica" themes. So now the time has come to get a bit more serious and reputable, having a more elegant and modern L&F. That's why I switched from the SyntheticaLookAndFeel
to the built-in SystemLookAndFeel
.
Ok, that was not as easy as I thought before, but anyway... I'm on the right path ;)
The only thing I'm struggling to get to work as it should, are those MenuItems
. Typically in Java, if you create a simple Menu or PopMenu and you add a JMenuItem, it should look like a normal menu having just a text in it. In our application I can do whatever I want, it ALWAYS shows a space on the left hand side of every menu item.
I searched the entire workspace for any (wrong) pre-set configuration. I checked the UIManager and tried out different ways of setting the menu items icons to nothing but I did not find anything useful. I even created a new class EmptyIcon
, which sets an icon with a width of 1px and set that as a pre-set into my UIManager.
// The empty icon class
public class EmptyIcon implements Icon {
private int width = 1, height = 0;
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return height;
}
}
// set this config in the gui configuration file
UIManager.put("MenuItem.arrowIcon", new EmptyIcon());
UIManager.put("MenuItem.checkIcon", new EmptyIcon());
Nothing helped so far. Does anybody know, where this behaviour could come from? Where should I look into, to find this misconfiguration?
Is there any way to force the ui to get rid of those icons?
To get a better idea you can find some pictures hereafter. This is the main menu bar:
This is a simple popup menu I just created:
And this is the code I tried to use in order to remove the gap:
jMenuItem1.setIcon(null);
jMenuItem1.setHorizontalAlignment(JMenuItem.LEFT);
jMenuItem1.setHorizontalTextPosition(JMenuItem.LEFT);
jMenuItem1.setIconTextGap(0);
jMenuItem1.setMargin(new Insets(0, 0, 0, 0));
jMenuItem1.setPressedIcon(null);
jMenuItem1.setText("TEST TEST TEST");
I tried to replicate this, and it turns out it is actually directly linked to the LookAndFeel, when it is changed to SystemLookAndFeel
(I'm running Win10).
Seems like this is (or was) a bug - see https://bugs.openjdk.java.net/browse/JDK-8152981 (reference taken from this thread).
This seems like it is intentional and I didn't find a way to completely remove the icon-spacing.
Adding this to the JMenuItem
helped though:
menuItem.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
This will change the component orientation, thus removing the space on the left side. Negative side-effect: it will move the space to the right side. Nonetheless, maybe this works out for you.
Another way I found, which I think only would work if you never use any Icons in your JMenuItem
, is like this:
item.setMargin(new Insets(2, -20, 2, 2));
You may tweak these values according to your needs. I don't know if these workarounds might have any side-effects, I just compiled what I found on the issue.