views:

59

answers:

3

How to record the number of mouse clicks in Java?

A: 

Generally speaking, you would attach a mouse click event handler, and whenever your handler gets called, you would increment an integer counter.

Douglas
+3  A: 

Something along the lines of this should do it, Adding the MouseListener on any component you want to listen for clicks on

public class MyFrame extends JFrame implements MouseListener{

   /** Number of times the mouse was clicked */
   private int clicks = 0;

   public MyFrame () 
   {
      this.addMouseListener(this);
   }

   public void mouseClicked(MouseEvent e) 
   { 
       //Increment click count
       clicks++;
   }

   public void mouseEntered(MouseEvent e) {}
   public void mouseExited(MouseEvent e) {}
   public void mousePressed(MouseEvent e){}    
   public void mouseReleased(MouseEvent e)  { }

}
Re0sless
+3  A: 

Note that the Java mouse events already have a click counter.

Dark Falcon