Handling Mouse Clicks - Java Swing

Java examples for Swing:Mouse Event

Description

Handling Mouse Clicks

Demo Code


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

import javax.swing.JButton;

public class Main {

  public void main(String[] argv) {
    JButton component = null;//from  w  w  w. j  av  a  2 s. c o  m
    component.addMouseListener(new MyMouseListener());
  }

}

class MyMouseListener extends MouseAdapter {
  public void mouseClicked(MouseEvent evt) {
    if ((evt.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
      //processLeft(evt.getPoint());
    }
    if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0) {
      //processMiddle(evt.getPoint());
    }
    if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
      //processRight(evt.getPoint());
    }
  }
}

Related Tutorials