Search code examples
javaswingprintingdelayjbutton

Program a button be pressed a maximum of once every 5 seconds in java


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);
    }
}

Solution

  • Program a button be pressed a maximum of once every 5 seconds in java