Prevent double click

How can I prevent my user to double click in one button?

Sometimes my user dont pay atencion on top bar (loading) and press the save button 2 times, and the method are executed 2 times, so how can I prevent this?


Does Button.setDisableOnClick(true) works for you?

Don`t work as expected, somethins he dont enable the button back :confused:

It should never enable back automatically, that is something you need to do yourself from the code. It is an inconvenience, but at least you don’t get double clicks :slight_smile:

The button needs to be enabled back programatically. Quoting the setDisableOnClick() javadoc: “Also, if developer * wants to re-enable the button, it needs to be done programmatically.”

Just please make sure that you call setEnabled(true) in your button’s onclick handler.

agh, there is workaround

	 * Disables button after 1st click, then enables it after certain time => 2nd click is performed over disabled button
	 * => double-click is disabled
	 * @param btnSubmit
	private static void preventDoubleClick(Button btnSubmit) {
		UI ui = UI.getCurrent();
		btnSubmit.addClickListener(event -> setTimeoutAsync(() -> {
			ui.access(() -> btnSubmit.setEnabled(true));
		}, 300));

	public static void setTimeoutAsync(Runnable runnable, int delay) {
		new Thread(() -> {
			try {
			catch (Exception e){
				log.error(e.getMessage(), e);

more correct fix (just instantly adds constructors ;))

public CustomButton extends Button {
	private ComponentEventListener<ClickEvent<Button>> doubleClickListener;
	private ComponentEventListener<ClickEvent<Button>> moreClickListener;

	public Registration addClickListener(ComponentEventListener<ClickEvent<Button>> listener) {
		return super.addClickListener(event -> {
			if(event.getClickCount() == 1) {
			} else if (event.getClickCount() == 2 && doubleClickListener != null) {
			} else if (event.getClickCount() > 2 && moreClickListener != null) {

	public GimsButton doubleClickListener(ComponentEventListener<ClickEvent<Button>> listener) {
		this.doubleClickListener = listener;
		return this;

	public GimsButton moreClickListener(ComponentEventListener<ClickEvent<Button>> listener) {
		this.moreClickListener = listener;
		return this;