This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes Flood Control (JButton) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Flood Control (JButton)" Watch "Flood Control (JButton)" New topic
Author

Flood Control (JButton)

Juan Manuel Alberto de los Santos
Ranch Hand

Joined: Jun 26, 2008
Posts: 48
Is there anyway to implement a "flood control" to a button ?

for example, if the button X is pressed more than 3 times in 5 seconds, print "wait a minute to reuse"

thanks !
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Would saving the time of the last n presses in a list do it?
Then design an algorithm to test for the desired conditions.
Juan Manuel Alberto de los Santos
Ranch Hand

Joined: Jun 26, 2008
Posts: 48
good idea !

help me here bro please


[ August 05, 2008: Message edited by: Juan Manuel Alberto de los Santos ]
Juan Manuel Alberto de los Santos
Ranch Hand

Joined: Jun 26, 2008
Posts: 48

[ August 05, 2008: Message edited by: Juan Manuel Alberto de los Santos ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Some possible improvements:
1) you're storing Integers in your List. You know this. Yet when you retrieve them, you convert them to string, then retrieve the int value from them. Something you already have:


2) store the result of System.currentTimeMillis() in your list. No need to create a Date object, format it, then call toString on a String (?) and then parse that as seconds. System.currentTimeMillis() already returns the current moment in time, with an accuracy of 10-20ms mostly:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Why don't you just disable the button until it is allowed to be pressed again?


GenRocket - Experts at Building Test Data
Juan Manuel Alberto de los Santos
Ranch Hand

Joined: Jun 26, 2008
Posts: 48
insane improvement Rob !

thanks a lot

**

Gregg Bolinger how can i do that ?

i know about, setEnabled(false) , but how can i "disable the button until it is allowed to be pressed again" ??
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

With a timer.

When the button is pressed you disable it, and start the timer. That runs for 3 seconds, then reenables the button and disables itself.

Check out javax.swing.Timer for more info.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Flood Control (JButton)