For a current project we need to allow a user only to press a button once every 5 or so seconds. We use a button to start a print job but we need to stop users from spamming the button and starting a dozen print jobs.
We are currently trying with the following code but it seems to que the clicks even when the button is disabled. So after a 5 second delay the clicks are registered even tough in that time the button is disabled.
private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {
Button1.setEnabled(false);
pressCount++;
System.out.println("Press count: " + pressCount);
PrintJob print = new PrintJob();
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(GUIFrame.class.getName()).log(Level.SEVERE, null, ex);
}
try {
print.PrintJob();
} catch (IOException ex) {
Logger.getLogger(GUIFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
Program a button be pressed a maximum of once every 5 seconds in java
have look at JButton#setMultiClickThreshhold(long threshhold)
proper way is add Swing Action
to the JButton
instead of [ActionListener][4]
and by using Swing Timer
to block isEnable