?
Solved

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

Posted on 2003-03-11
14
Medium Priority
?
239 Views
Last Modified: 2010-03-31
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
Comment
Question by:Phoinix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8113149
Listening...
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8113150
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
 
LVL 2

Expert Comment

by:antonsigur
ID: 8113163
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 2

Expert Comment

by:antonsigur
ID: 8113197
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
 

Author Comment

by:Phoinix
ID: 8113200
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
 
LVL 2

Expert Comment

by:antonsigur
ID: 8113237
I would use actionListener on the puttons...
Why do you use MouseListener?
0
 

Author Comment

by:Phoinix
ID: 8113273

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
 
LVL 2

Expert Comment

by:antonsigur
ID: 8113302
Are you using Button or JButton ?
Then just do b1.addActionListener(this);

and implement actinlistener instead of mouselistener
0
 

Author Comment

by:Phoinix
ID: 8113309
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
 
LVL 2

Accepted Solution

by:
antonsigur earned 400 total points
ID: 8113450
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
 

Author Comment

by:Phoinix
ID: 8113588
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
 
LVL 2

Expert Comment

by:antonsigur
ID: 8113671
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
 

Author Comment

by:Phoinix
ID: 8113695
how do you set text to a char variable?
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8113736
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question