Enable right click in jFrame

两盒软妹~` 提交于 2019-12-01 13:09:07

问题


Hye there I am searching for how to enable (only and only) right click and show a popup menu in jFrame using NetBeans; using my code as:

    private void formMouseClicked(java.awt.event.MouseEvent evt){                                  

            pop.show(evt.getComponent(),evt.getX(), evt.getY());
     }      

I'm still unable to get the best results because it also popups on left click too suggest me any hint that I'm doing any blunders Thanks in advance.


回答1:


You can use:

if (evt.isPopupTrigger()){
//Show menu and do stuff here
}



回答2:


This is what it should done like this truly It's amazing and helpful

        private void textfiledMousePressed(java.awt.event.MouseEvent evt) {                               

             if (evt.getModifiers() == MouseEvent.BUTTON3_MASK){  
                p.show(evt.getComponent(), evt.getX(), evt.getY());
             }
       }

don't forget to add an import for

        import java.awt.event.MouseEvent;

have fun...



来源:https://stackoverflow.com/questions/16198383/enable-right-click-in-jframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!