0

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.

Tech Nerd
  • 822
  • 1
  • 13
  • 39

2 Answers2

0

You can use:

if (evt.isPopupTrigger()){
//Show menu and do stuff here
}
0x6C38
  • 6,796
  • 4
  • 35
  • 47
  • Sir nothing happens at all i have used private void formMouseClicked(java.awt.event.MouseEvent evt) { if (evt.isPopupTrigger()){ pop.show(evt.getComponent(), evt.getX(), evt.getY()); }} – Tech Nerd Apr 24 '13 at 17:31
  • `evt.isPopupTrigger()` returns `false`? – 0x6C38 Apr 24 '13 at 17:32
  • actually yes now i have used ! (not operator) and it works but same problem is still there the left mouse click is also working and i only want the right click to work Please help me – Tech Nerd Apr 24 '13 at 17:33
  • thats very strange, it shouldn't return false, you are probably gonna need to debug – 0x6C38 Apr 24 '13 at 17:37
  • I am using netbeans 7.3 but still the prob is not to going so far my left click is still working which creates a problem for me i actually want to disable left click popup – Tech Nerd Apr 24 '13 at 18:09
  • if you are using the exact code I posted you should either be getting right click only popupMenu or no popUpMenu. Also would you mind not copying my profile image, that 3d model wasn't easy to find and there is thousands of java images all over the internet – 0x6C38 Apr 24 '13 at 18:13
  • Ok sorry for it i will surely remove it sir but i have copied the exat code then inside if i have placed the show method but still i get the left click enable – Tech Nerd Apr 24 '13 at 18:42
0

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...

Tech Nerd
  • 822
  • 1
  • 13
  • 39