I have a JContextMenu that I open in a JDialog by calling (the MouseListener is registered to the JDialog):
menu.show(e.getComponent(), e.getX(), e.getY());
The menu itself has an ActionListener registered on the menuitems. It opens fine and the actions are performed as expected, until I open another menu (for example the application's main menu or a context menu on another component) that overlaps something else. For example if a menu in the main menu bar overlaps a dialog or if a context menu is opened that reaches outside the window. Then the clicks on the context menu of the JDialog just close the context menu, as if you had clicked beside the menu to close it (the other menus in the application are still fine though).
It's as if the click goes through the menu. If I select a menu item with the arrow keys and press enter, it does the action just fine.
If I specify e.g. the main window of the application as invoker in the menu.show()
, then it seems to always work (but of course it opens on the wrong location). What seems to work is registering the mouse listener on the content pane of the JDialog instead.
Can anyone explain what can cause this behaviour? Should a JDialog not be used as invoker of a context menu for some reason?
I also tried using setComponentPopupMenu()
, but that only seems to exist for JComponent and not JDialog or it's content pane. Using it on a component inside the JDialog works, but so does the other method of using a mouse listener and menu.show()
.