InputEvent.BUTTON3_MASK (for right mouse button) : Mouse Event « Swing Event « Java Tutorial





import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class Main {
  public static void main(String[] argv) throws Exception {

    JTextField component = new JTextField();
    component.addMouseListener(new MyMouseListener());
    JFrame f = new JFrame();

    f.add(component);
    f.setSize(300, 300);
    f.setVisible(true);

  }
}

class MyMouseListener extends MouseAdapter {
  public void mouseClicked(MouseEvent evt) {
    if ((evt.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
      System.out.println("left" + (evt.getPoint()));
    }
    if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0) {
      System.out.println("middle" + (evt.getPoint()));
    }
    if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
      System.out.println("right" + (evt.getPoint()));
    }
  }
}










15.28.Mouse Event
15.28.1.Mouse event popup menu triggered
15.28.2.Right click to trigger the popup menu
15.28.3.Using SwingUtilities for Mouse Button IdentificationUsing SwingUtilities for Mouse Button Identification
15.28.4.Uses a subclass of MouseInputAdapter to listen to mouse events and mouse-motion eventsUses a subclass of MouseInputAdapter to listen to mouse events and mouse-motion events
15.28.5.Handling Mouse Clicks
15.28.6.InputEvent.BUTTON1_MASK (for left mouse button)
15.28.7.InputEvent.BUTTON2_MASK (for middle mouse button)
15.28.8.InputEvent.BUTTON3_MASK (for right mouse button)
15.28.9.Detecting Double and Triple Clicks