<Swing Dev> Issue with setComponentPopupMenu
Van Den Borre, Koen
Koen.VanDenBorre at esko.com
Thu Oct 5 10:32:14 UTC 2017
Hey,
I am seeing an issue when using setComponentPopupMenu.
When you use setComponentPopupMenu in a window and create a modeless dialog and position that over that window. A contextual menu click in the modeless dialog will show the contextual men of the component in the parent window.
I traced the issue down to processMouseEvent in the Container class. There, a call is made to trackMouseEnterExit which will create and dispatch a MouseEvent.MOUSE_ENTERED mouse event.
This generated mouse event still has the isPopupTrigger attribute set to true which will then triggers the componentPopupMenu to be shown.
As always an example says more that a thousand words:
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
public class ComponentPopupMenuTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
test();
}
});
}
private static void test() {
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(new JMenuItem("You should not see me"));
JFrame frame = new JFrame("Main Frame");
frame.getRootPane().setComponentPopupMenu(popupMenu);
frame.setSize(400, 400);
frame.setLocation(0, 0);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JDialog dialog = new JDialog(frame, "Modeless Dialog", false);
dialog.getContentPane().add(new JLabel("Do a contextual mouse click here"));
dialog.setSize(300, 300);
dialog.setLocation(50, 50);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
}
Please be advised that this email may contain confidential information. If you are not the intended recipient, please notify us by email by replying to the sender and delete this message. The sender disclaims that the content of this email constitutes an offer to enter into, or the acceptance of, any agreement; provided that the foregoing does not invalidate the binding effect of any digital or other electronic reproduction of a manual signature that is included in any attachment.
More information about the swing-dev
mailing list