How do I create a right click context menu in Java Swing?

后端 未结 5 1384
长情又很酷
长情又很酷 2020-11-28 20:51

I\'m currently creating a right-click context menu by instantiating a new JMenu on right click and setting its location to that of the mouse\'s position... Is t

5条回答
  •  渐次进展
    2020-11-28 21:22

    This question is a bit old - as are the answers (and the tutorial as well)

    The current api for setting a popupMenu in Swing is

    myComponent.setComponentPopupMenu(myPopupMenu);
    

    This way it will be shown automagically, both for mouse and keyboard triggers (the latter depends on LAF). Plus, it supports re-using the same popup across a container's children. To enable that feature:

    myChild.setInheritsPopupMenu(true);
    

提交回复
热议问题