Problems when buttons in Vaadin 14 are clicked more than once

We have buttons that generate data that take about 15 seconds to generate, while this is happening our users can click the buttons over and over again. Unfortunately, the data is generated more than once.

Therefore, I would like to ask if it is possible to prevent multiple clicks on the same button while the server is executing the request?

DId you try setDisableOnClick(true)?

Yes, but then the button is deactivated after one click, which is not very useful because, for example, if an error message appears when required data has not been entered, the user must be able to click on the button again.

Well… of course… it’s your job to activate it again once your long running task is finished. button.setEnabled(true).

We would have to implement this several times. Are there any other possibilities to solve this problem?

Sure, simplest solution - error handling is your part ;)

  class MyButton extends Button {
    
    public MyButton(String text, Runnable action) {
      super(text, e -> {
        action.run();
        e.getSource().setEnabled(true);
      });
      setDisableOnClick(true);
    }
  }

2 Likes

Here is a bit more complete example of async task runner button

There is slightly different version of similar thing included in Viritin too.