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");