0
JButton btnNewButton = new JButton("CLICK ME!");
btnNewButton.setBounds(134, 142, 274, 77);
btnNewButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {                
    clicked++;
    String x=Integer.toString(clicked);
    textArea.setText(x);                                            
    }       
});

I'm stuck here I want to create a program in GUI that counts the number of button click in specific time for example the timer start then count the number of clicks when the loop stop the button click is not working or stop counting the clicks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Volkswagen
  • 155
  • 2
  • 4
  • 11
  • 3
    1) `btnNewButton.setBounds(134, 142, 274, 77);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Mar 24 '14 at 11:11
  • That's a great story, but what's the question? – Paul Samsotha Mar 24 '14 at 11:14

3 Answers3

1

There are two possible solution

1.Make the button clickable when timer starts and unclickable when timer stops

Or

2.also u can use flag to check whether timer is running Or not.If timer is running make flag true when gets over make it false. Somthing like below snipet

public void actionPerformed(ActionEvent e) {     
if (flag) {
    clicked++;
    String x=Integer.toString(clicked);
    textArea.setText(x);                                            
 }    
else
{
 // doSomething
}       
}
Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
0

You can have some boolean variable which says if it's time for clicking (set on true when timer is started and on false when time's up). Then count clicks when this variable is true:

public void actionPerformed(ActionEvent e) {     
    if (timeIsRunning) {
        clicked++;
        String x=Integer.toString(clicked);
        textArea.setText(x);                                            
    }           
}
ICeQ
  • 150
  • 11
0

https://stackoverflow.com/a/9413767/1306811

Have a click counter.

private int clickCounter = 0; // as an example

Create get/set methods for it.

Add a MouseListener to your JButton so you can effectively track click events (MouseEvent arg0, arg0.getClickCount(), etc). At the end of each call to mouseClicked increment the clickCounter (clickCounter += arg0.getClickCount(), as an example).

Modify the answer linked so that clickCounter is set to 0 at every "time step" (however long you want it to be).

Voila.

Community
  • 1
  • 1
Gorbles
  • 1,169
  • 12
  • 27