Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

How do i represent an action listener for a button to respond depending on the number of clicks within a timeframe?

Okay, what to give you a brief picture, im trying to create a simulation of a mobile phone .
I have a button(just one for now, since the others will implement the same code), that contains the letters 1, a, b, and c. How do i get an actionListener to respond to events like a real phone. A user wants to type a message, so he has all three options on the same button, so he clicks once for 1, twice for (a), thrice for (b), and and four times for (c), all within a time limit. If the time  is too long it refreshes back to the default (1).
Do you see what i mean? hopefully. So any advice as to how i do this?
Thanks.
Phoinix
0
Phoinix
Asked:
Phoinix
  • 8
  • 5
1 Solution
 
CEHJCommented:
Listening...
0
 
antonsigurCommented:
int b1Clicked = 0;


//the buttom is clicked
b1Clicked++;
if (b1Clicked > 3)
  b1Clicked = 0;

if (b1Clicked == 0) {
  //do something..
} else if (b1Clicked == 1) {
//do the other thing...
}

So, you just have buttom named b1, and a int named b1Clicked, that counts how often the buttom has been pressed, simple eh?
0
 
antonsigurCommented:
Then you need a timer to refresh all buttoms to 0 if they are not clicked... Do you need a implemention of the timer?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
antonsigurCommented:
You could use one single long variable:

int refreshDelay = 4000; //four seconds
long timer = 0;

when the application starts , you initalize it with timer=System.currentTimeInMillis();

.....

Now in your actionlistener, you would do

long now = System.currentTimeInMillis();

if ( (now - timer) > refreshDelay) {
  b1Clicked = 0;
  // and same with other buttoms...
}
timer = now; //Reset the timer
0
 
PhoinixAuthor Commented:
thanks.. err.. how would i use MouseListener ? ..
basically the class implements MouseListener

....
public class MobilePhone extends JFrame implements MouseListener
{
....
....
button.addMouseListener(this);
...
}
public void MouseClicked(MouseEvent e)
{
...
}

Something like that right?
0
 
antonsigurCommented:
I would use actionListener on the puttons...
Why do you use MouseListener?
0
 
PhoinixAuthor Commented:

i figured that its whenever the mouse is clicked, so i thought i had to use mouseListener..
Is it possible to do it using ActionListener?
0
 
antonsigurCommented:
Are you using Button or JButton ?
Then just do b1.addActionListener(this);

and implement actinlistener instead of mouselistener
0
 
PhoinixAuthor Commented:
antonsigur

could you possibly tell me how to implement that timer in code, as part of an ActionListener?
So, it would like this then:

...
public class MobilePhone extends JFrame implements ActionListener
{
...

JButton b1 = new JButton;
b1.addActionListener(this);
int b1clicked = 0;
...
}
public void ActionPerformed(ActionEvent e)
{

... i dont understand how to write this bit .. could you explain how youre code works and how to implement it?
}
0
 
antonsigurCommented:
Here it is all together, half-implemented for one buttom... You should do the rest now!


import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class prufa2 extends JFrame implements ActionListener
{
//...

JButton b1;
int b1clicked = 0;
int refreshDelay = 4000; //four seconds
long timer = 0;
   
    public prufa2() {
        b1 = new JButton("b1");
        b1.addActionListener(this);    
    }
//...


    public void actionPerformed(ActionEvent e)
    {
        long now = System.currentTimeMillis();
       
        if ( (now - timer) > refreshDelay) {
         b1clicked = 0;
         // and same with other buttoms...
        }
        timer = now;   //Sets the timer to NOW!    
       
        if ( e.getSource() == b1 ) {
            if (b1clicked == 3)
                b1clicked = 0;
            b1clicked++;
            switch (b1clicked) {
            case 1:
             //Todo if key is one time
               break;
             case 2:
             //Todo if case is two times...
               break;
            }
         
        }
    }
}
0
 
PhoinixAuthor Commented:
Thanks a lot! That code is great.
Yep, i'll do it for all the other buttons.

However currently it loops between all the possible cases , i.e. i click it once, it shows 1, then i click again, it shows "a", then i click it again, it shows "b", etc. So the display updates to :
1abc1abc1abc
for as many times that i click
how do i make it so that if if click  within a certain timeframe it will jump ahead and choose just say "b". Currently in order to get to say "b" i have to click three times, and that means that "1","a" and then "b" are all put on the screen.

0
 
antonsigurCommented:
You could use the timer:
Setup a global char variable, name thePrintout!


In the //Todo...  you update the variable thePrintout to the current character. (1abc..)

Now, if you press other buttom (thePrintout don't have a character that this buttom is using), or if the timer goes off ((if ( (now - timer) > refreshDelay)))

then thePrintout is THE printout on the screen. and the buttom is resetted to 0 (as it is now)

 - hope this helps
0
 
PhoinixAuthor Commented:
how do you set text to a char variable?
0
 
antonsigurCommented:
char someVar = 'a';
someVar = 'b';

etc...

You can browse the http://java.sun.com/j2se/1.4.1/docs/api/ for java functions and classes available and ready for use.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now