This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Flood Control (JButton)

 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ August 05, 2008: Message edited by: Juan Manuel Alberto de los Santos ]
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you just disable the button until it is allowed to be pressed again?
 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic