The moose likes Swing / AWT / SWT / JFace and the fly likes MouseListener / Right Click ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "MouseListener / Right Click ?" Watch "MouseListener / Right Click ?" New topic
Author

MouseListener / Right Click ?

Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Does anyone know how to write the code for the following:
Lets sat you have the button:
JButton button = new JButton("Right Click me");
How do you write a mouse listener so that when you right click on it the following fucntion is executed :
System.out.println("thanks");
Thanks in advance


yours respectfully<br />Avin Sinanan
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4120

All you have to do is add a MouseListener to the component that defines what to do in it's mousePressed method. In that method it should test isMetaDown on the MouseEvent it recieved. The code below illustrates one way to do this...



-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
IntelliJ Java IDE
 
subject: MouseListener / Right Click ?
 
Threads others viewed
writing ActionEvent to jButton
Identifying GUI events
Button Renderer problem in Java swings
Right-clicking a Button or JButton
Opening/Calling up applet window from an Applet application
developer file tools