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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sameerjoshiCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.