Way2Java

Mouse Multiple Clicks getClickCount()

getClickCount() of MouseEvent class returns the number of clicks done on mouse.

Following is the method signature as defined in class java.awt.event.MouseEvent

Mouse Multiple Clicks getClickCount() program uses getClickCount() to know the number of clicks done on the button.
import java.awt.*;
import java.awt.event.*;
public class MultipleClicks extends Frame
{
  Button b1;
  public MultipleClicks()
  {
    setLayout(new FlowLayout());
    b1 = new Button("CLICK MANY TIMES");

    b1.addMouseListener(new HelperListener());

    add(b1);
    setSize(250, 250);
    setVisible(true);
  }
  public static void main(String args[])
  {
    new MultipleClicks();
  }
}
class HelperListener  extends MouseAdapter
{
  public void mouseClicked (MouseEvent e) 
  {
    int count = e.getClickCount();
    switch(count)
    {
      case 1:
        System.out.println("It is single click");   break;
      case 2:
        System.out.println("It is double click");   break;
      case 3:
        System.out.println("It is triple click");   break;
      default:
        System.out.println("It is multiple clicks");   
    }
  }
}



Output screen of Mouse Multiple Clicks getClickCount() /center>

A switch statement is preferred to display the number clicks performed by mouse.

Also know about mouse right click and double click.

View All for Java Differences on 90 Topics