Disable double click

Hello all,

First of all sorry of my poor language skill.

Is there any kind of solution for disable double click in a Button at the vaadin 7 ? Because if I clicked double at the button which opening a new pop up window, the window opening twice.

Thank you!

Hi,

maybe you can check if the window is open and not open if that’s true?

-Olli

Not a wrong idea! I used this one a couple weeks ago.
But now I have to use this more than 200 other place. That is the reason why I try to find global solution.
Btw, thank you! :slight_smile:

It might be possible to do a client-side extension for preventing a double click, but I’m guessing it’s still probably easier and more reliable to check that on the server side.

-Olli

There is a function on buttons called setDisableOnClick that you might find interesting?

So, the solution for me.
I create handlingOpenWindow() method, which I call at the buttonClick. That method set the button setEnabled(false), and containing a window.addCloseListener where I change the button setEnabled(true). The method inside parameters are “Window window, Button button”.

Only one problem with this. I have to check the whole system where we are use window, which opening by button click.

BTW Thank you guys.

I think you’re going to need to go through all the usages no matter what the fix is :frowning:

-Olli

Yes, it is probably true! :confused:

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.setDisableOnClick(true);
		btnSubmit.addClickListener(event -> setTimeoutAsync(() -> {
			ui.access(() -> btnSubmit.setEnabled(true));
		}, 300));
	}

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

more correct fix (just instantly adds constructors ;))

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

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

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

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