is there any way to remove the empty icons gap in a jmenuitem java swing?

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.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top