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

  • public int getClickCount(): Returns the number of mouse clicks associated with this event.
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");   
    }
  }
}


Mouse Multiple Clicks getClickCount()
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

2 thoughts on “Mouse Multiple Clicks getClickCount()”

  1. Unless I’ve misinterpreted the intent, there is a problem…

    A double click is reported as BOTH of the following:
    It is single click
    It is double click

    And a triple click is reported as ALL of the following:
    It is single click
    It is double click
    It is triple click

    Surely the objective should be to distinguish among the three distinct possibilities, reporting:

    EITHER
    – a single click
    OR
    – a double click
    OR
    – a triple click

    Anything short of that is a pretty useless example of getClickCount().

Leave a Comment

Your email address will not be published.