JPopupMenu removing borders / insets

Hi,

Well Swing is 2D so please forgive if this thread doesn’t belong here. I need to customize my menus. I have a popup menu but, though I remove the borders, there is still space at the top and bottom of the JPopupMenu. There is also a white line at the left side and a grey line (or component?) on the right of the JPopupMenu. How can I remove this space / lines from a JPopupMenu?

Devyn

The following is for the Metal look. Other looks might do stuff differently. Let m be a JPopupMenu. If you do m.setBorderPainted(false) and m.setBackground(Color.RED) you’ll see the additional border of the JPopupMenu. You’ll see m’s insets, also shown by System.out.println(m.getInsets()).

You cannot manipulate the insets directly, but if you look into the JComponent code, you’ll see that the insets are calculated from the border. So m.setBorder(BorderFactory.createEmptyBorder()) does the trick.

There’re still parts of a border shown, but these are the borders of the menu items. Switch off their borders with mi.setBorderPainted(false) and you’re done.

Hi sma,

Thank you for looking into this. m.getInsets() prints “0, 0, 0, 0” for all insets. I use m.getMargins() to get insets too and that too produces “0, 0, 0, 0” at runtime. I have m.setBorderPianted(false). Creating an empty border had no effect but the space at the bottom of the JPopupMenu still shows up. This may be a PLAF question. I’m using .getPopupMenu() to retrieve the JPopupMenu from it’s parent. Unless .getPopupMenu() returns “return new JPopupMenu();” this continues to vex me.

Devyn

[quote] This may be a PLAF question.
[/quote]
I used Metal of JDK 1.4.2 beta and was able to remove all borders for a popup menu create with new JPopMenu() . getPopupMenu() will create a new JPopupMenu once and returns the identical instance thereafter. Removing the border also works in that case.