-1

I am slowly stepping into Java and trying out a program using Swing. I am trying out a program to use a single button to enter different letters. Each letter should be entered in the text box within 5 seconds.

I've tried using Handlers, but unfortunately I messed it up. What I am trying to achieve is that, when I tap the button once, Letter A should be entered and when pressed twice Letter B should be entered and thrice Letter C. Most importantly, time to enter each letter is 5 seconds. After 5 seconds, if the button is pressed once Letter A should be entered. Hope you understand my question.

How to use same button for different tasks with respect to time?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kaushik
  • 553
  • 3
  • 11
  • 27
  • My idea: create a boolean flag and a counter. On first button click put Thread (handling calling the function) to sleep for 5 seconds and increment a counter. On second/third click check your flag (to make sure you don't expand your thread's nap) and increment counter. Use that counter to check how many times button was clicked. – spoko Aug 24 '14 at 10:50
  • Welcome to Stack Overflow, please take the [Tour](http://stackoverflow.com/tour). What have you tried so far? Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). – DavidPostill Aug 24 '14 at 10:52
  • I've tried using Handlers to reset the variable with respect to time DavidPostill. Just like using "Exit on double back button press in Android" . But its throwing me some errors – Kaushik Aug 24 '14 at 10:59
  • 1
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Aug 24 '14 at 11:04
  • What, so for 'z' the poor user needs to hit the button 26 times? What if they want a 'y' (which would demand pressing the button every 1/5 of a second or less for 25 presses) and accidentally hit it 26 times? This seems to be another unusable GUI in the making. If it is a game, I'm guessing it is a frustrating one. – Andrew Thompson Aug 24 '14 at 11:13
  • No I'm just trying to learn things in Java based on time constraints. – Kaushik Aug 24 '14 at 11:15

1 Answers1

0
  button.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

    JButton buttonPressed = (JButton) event.getSource();
     if (lastButtonPressed == buttonPressed){
        //BUTTON pressed more than 2 times
        count++;
        System.out.println(" has been pressed " + count + "times in a row.\n");
         }
         else
         {  
        count = 1;
        System.out.println(buttonPressed.getText() + " has been pressed.\n");
         }
    lastButtonPressed = buttonPressed;
        }
       Timer t = new Timer(5000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                 // add what u want to do with button
                 //if(count ==2){
                 // print (b)
                  //    }
                }
            });

            t.start();
        }

    });

Check this post Clicking a JButton twice in a row

and edit your code accordingly. eg

 if(count ==2){
  // print (b)
  }
Community
  • 1
  • 1
Meesh
  • 508
  • 1
  • 5
  • 17
  • 1
    You didn't implement "5 seconds" part. – spoko Aug 24 '14 at 11:09
  • 1
    Thanks for you time. But I'm trying to enter each letter within 5 seconds. That's the main issue I have. – Kaushik Aug 24 '14 at 11:09
  • @KaushikS: Why not simply use, [getClickCount()](http://docs.oracle.com/javase/8/docs/api/java/awt/event/MouseEvent.html#getClickCount--) from [MouseEvent](http://docs.oracle.com/javase/8/docs/api/java/awt/event/MouseEvent.html) class for this purpose – nIcE cOw Aug 24 '14 at 12:16