Pressing button Twice

The application is suppose to exit when a button is clicked on twice. How do I check to see if the button has been clicked twice?
[143 byte] By [hyee1] at [2007-9-26 12:21:54]
# 1
if you use a mouse listener on the button, you can just use the method: MouseEvent.getClickCount() ... if the user double-clicks, itll return 2.
sberardi at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
How would you do it if you used addActionListener? On my assignment it doesn't say to use a mouseListener.
hyee1 at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
Use a flag. Set it to true after the first click and if the second click is on the same button close the app, otherwise clear the flag.
jquattro at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4

I still don't understand how the second click will be known. This is what I have.

public void actionPerformed(java.awt.event.ActionEvent e) {

txtaDisplay.setText("You Clicked The " + e.getActionCommand() + " Button.");

if (e.getSource() == btnSouth ) {

isbtnSouthPressed = true;

}

else {

isbtnSouthPressed = false;

}

}

hyee1 at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5
in actionPerformed()e.getClickCount() will give the count, check if its greater than 2if( e.getClickCount() >= 2){// do what you wanted.}don't forget the award me if it helped.
bharatchhajer at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 6

> in actionPerformed()

> e.getClickCount() will give the count, check if its

The actionPerformed method takes an ActionEvent as parameter which has no getClickCount() method. In this case you have to use a flag:

//declare a flag variable

private int clicked = 0;

//code in actionPerformed(ActionEvent ae) method

if(ae.getSource().equals(okButton)){

if(clicked == 1){

System.exit(0);

//clicked = 0; if you do not exit here clear the flag

}else{

clicked++;

}

}else{

clicked = 0;

}

When you compare object it is best to use "equals" and not "==".

> don't forget the award me if it helped.

?

jquattro at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 7

myThing.addMouseListener(new java.awt.event.MouseAdapter() {

public void mousePressed(java.awt.event.MouseEvent evt) {

long xyz = System.currentTimeMillis() - myLastClick;

if(xyz < 500) {

epicChosen();

}

myLastClick = System.currentTimeMillis();

}

}

);

private long myLastClick = System.currentTimeMillis();

benmorgan at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 8

Sorry, I pressed post instead of preview. I was going to offer some explanation as well.

I asked this same question a while back and I didn't just want to know if there had been two clicks but if the user had actually made a double click i.e. two clicks a very short time apart.

Someone helped me then and this is what I got:

private long myLastClick = 0.0;

myObject.addMouseListener(new java.awt.event.MouseAdapter() {

public void mousePressed(java.awt.event.MouseEvent evt) {

long timeBetweenClicks = System.currentTimeMillis() - myLastClick;

if(timeBetweenClicks < 500) {// half a second

doSomething();

}

myLastClick = System.currentTimeMillis();

}

}

);

When the button is clicked the time is stored and then next time it is clicked if the timeBetweenClicks is less than your threshold (I set it at 0.5 seconds) then it will run the desired code.

Hope this is helpful :)

Ben.

benmorgan at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 9
If you use a MouseListener than the already suggested method is the best option: getClickCount(). You don't have to do any calculations, everything is built-in.
jquattro at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 10
Thanks everyone for your help. jquattro, I rewarded you!
hyee1 at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 11
Thanks!
jquattro at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 12

jquattro,

How come everyone people get so upset on this forum? I'm new to learning java and by reading that more adv java programmers get irritated with stupid questions makes me not want to post questions. I thought people were suppose to help you on here no matter how stupid the question is. I'm asking you this b/c I saw your name on a message I read. It was a guy asking a question not pertaining to java. I think the user name was like jaffasoft? Anyways, just wondering.

hyee1 at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 13

hyee1:

First of all the jaffasoft guy was asking a question about JavaScript. He should go to another forum for that scripting language or read a book. It is not that difficult. Secondly, he had a little attitude problem and used cuss words in this forum. That is not cool. He didn't even know HTML that well, and yet he was asking a question about JavaScript which manipulates the DOM(Document Object Model) of the HTML. If he would have asked a question about JAVA, maybe he would have gotten a better response..

Kent

runtojesus at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 14

Hi,

To understand my answer you were reading take a look at this thread.

http://forum.java.sun.com/thread.jsp?forum=54&thread=189759

This was the initial posting of jaffasoft. And after I gave him some hints, and he also received more help from kksenji, he posted again with this text:"Obviously the dickhead that gave it to me, didn't give one that would work on all fields without all this bullshit about script signing tools."

You can read the entire thread to see what is all about. Anyway the point is I am willing to help but I would not accept this kind of language, even more after I spent time trying to give a solution.

jquattro at 2007-7-2 3:00:25 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 15

I understand both of y'alls views, but what about this one? http://forum.java.sun.com/thread.jsp?forum=54&thread=189777 I could understand the irritations for repetitive questions, but what about the ones that aren't, but y'all think they are stupid? I don't jump to this forum right when I can't figure out a problem, but I would like to know the more adv programmers are willing to help even if the questions are stupid to y'all. jquattro, the one you helped me on I was trying to figure that out since last week. It was just not clicking in my head to do the inner if statement.

hyee1 at 2007-7-2 3:00:26 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 16

If people are polite and willing to contribute to solving their problem (showing where they have got to, using the search forum facility before posting) they'll always get help.

If they are rude and/or expect other people to do their job/homework for them without making any effort, they may get helped but they may also get told to behave more suitably.

Your postings were clearly in the polite/contributing class so you get lots of help.

QED

gseel at 2007-7-2 3:00:26 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 17

I'll, try to explain.

Well, I wouldn't call a question stupid, maybe some questions are asked without any previous thinking and for this reason some people gets irritated. If I see repetitive question sometimes I answer them, if I have time, sometimes I simply ignore them, but I'm not getting angry for this reason. Other people do and I understand that: the one who posts a question should do a previous search and if he doesn't find an answer than he should post it. But generally people tends to be a little lazy, so they just post and wait for the answer.

In my case I never posted a question in a forum, I always tried to figure it out and if I can't then I look for answers in forums or simply by doing some web searching, and I usually find what I need. I don't say that this is the way to go.

So, I am willing to help, if I do it I don't expect any "dollars", and I am glad if I can bring some solutions to the poster. What I don't accept is the kind of language you saw in the thread we were talking about.

I hope you can understand what I mean.

jquattro at 2007-7-2 3:00:26 > top of Java-index,Archived Forums,New To Java Technology Archive...