Re: Mouse click/double click effect

Hi,
Hi there,

I want to use themultiple mouse clicks using my program.  If I click an object once for the first time,  it should print "Print # 1" and second time "Print # 2" and so on.

But every time it is printing "Print # 1". My piece of code is below.

If anybody can help.

Thanks in advance.

Med
-------

public void Method {
       int click = 0;
       if (click == 0) {
          System.out.println("Print # 1");
          click++;
       }
       else if (click == 1) {
          System.out.println("Print # 2");
          click++;
       }
}
MedAsked:
Who is Participating?
 
sameerjoshiConnect With a Mentor Commented:
Try this...I could not find better solution than this....

1) Approach one :

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class d extends Applet
implements MouseListener {
                           
public void init() {
    this.addMouseListener(this);
    }
                           
public void paint(Graphics g) {
    g.drawString("Click here", 10,10);
    }

public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
    System.out.println
    ( "Click at (" + e.getX() + ":" + e.getY() + ")" );
    if (e.getClickCount() == 2)  
    System.out.println( "  and it's a double click!");
    else
    System.out.println( "  and it's a simple click!");
    }
}



2) Approach Two:

After first click set an average time when the user is expected to click for the second time. , say 1 sec. If he doesnt click with n 1 sec it can be justified that it is an single click else it is a double click.

0
 
heyhey_Commented:
public void Method {
       int click = 0;
       if (click == 0) {
.....


'click' is local variable that is initialized every time when Method is called. use some 'external' variable / object field.

int click = 0;
public void Method {
       if (click == 0) {
.....
0
 
SergeiTXCommented:
Yes. That will work from time to time. Problem with JDK (from Sun) that there is long time bug - mouseClicked doesnt work each time, when you actually click mouse button.

use mousePressed instead :) That will do it.
0
All Courses

From novice to tech pro — start learning today.