views:

32

answers:

1

Hi,

When I let popup a JPopupMenu without passing the "invoker component", the menu doesn't work: submenu's don't open and isn't getting repainted. But when I create a completely useless JFrame with a JLabel inside, and I pass the JLabal as invoker, it works correctly...

Any suggestions, how to avoid creating a useless frame. And my application really hasn't any frames open, it just has to popup a simple menu.

JPopupMenu.show(null, xOnTheScreen, yOnTheScreen); // Doesn't work
JPopupMenu.show(aStupidJLabelInAStupidJFrame, x, y); // Works

Thanks

+1  A: 

Take a look at JPopupMenu source code and you'll see why you have to set an invoker.

Showing a popup menu without any existing component would be very bad usability, in the same league as popup windows from a browser.

Why can't you use JComponent#setComponentPopupMenu, or add a mouse listener to the component in which you want to show popup menu?

Geoffrey Zheng
I took already a look in the code, but can't see why...
Martijn Courteaux
Search for `JPopupMenu#getInvoker` in JDK6 you'll see a whole bunch of references. It's really not a good idea to change such a basic behavior and assumption.
Geoffrey Zheng