2

I'm learning Vaadin framework. I'm trying to capture double click event for the item click listener. But it's not working as expected. Please refer the code below,

grid.addItemClickListener(e -> {
        if(e.isDoubleClick()) {
            System.out.println("Double click");
        } else {
            System.out.println("Single click");             
        }
});

When I do double click on the grid item, it is only considered a single click.

Pearl
  • 384
  • 1
  • 8
  • 15

1 Answers1

4

As mentioned in Doubleclick listener on Vaadin Grid the problem is the setEditorEnabled(true) as this prevents the DoubleClick-Event being fired (as it seems like a double click event on grid is a trigger for Vaadin to interally make the editor visible).

I created a workaround which seems to work (you should test/evaluate that everything really works as intended), so that you have both:

  1. The possibility to doubleClick and add a Listener that reacts on doubleClicks
  2. have Editor enabled on the grid

The trick is to initially disable the editor (it is disabled by default) and then enable it "on your own" inside the ItemClickListener (if e.isDoubleClick()).

Then you have to use a class that extends Grid and overrides the method doCancelEditor(). Inside this method (which is called when the cancel Button is clicked and after the save Button is clicked (after the commit)) you then disable the editor again after the cancel and/or save button is pressed.

ExtendedGrid:

public class ExtendedGrid extends Grid {

    @Override
    protected void doCancelEditor() {
        super.doCancelEditor();
        setEditorEnabled(false);
        System.out.println("Editor disabled during doCancelEditor");
    }
}

MyUI:

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        Grid grid = new ExtendedGrid();
        BeanItemContainer<Person> container = new BeanItemContainer<>(Person.class);
        container.addBean(new Person("marco", "test"));
        grid.setContainerDataSource(container);
        grid.addItemClickListener(e -> {
            if(e.isDoubleClick()) {
                grid.setEditorEnabled(true);
                BeanItem item = (BeanItem) e.getItem();
                grid.editItem(item.getBean());
                System.out.println("Double click");
            }
        });
        setContent(grid);
    }
codinghaus
  • 2,218
  • 1
  • 14
  • 19
  • Thanks @codinghaus. This solution works fine while we doing double click, but it is also invoking the functionality which i written for single click also. For example, for a single click we have the popup window for the record which shows information in detail. So, the double click invokes the functionality of both single click and double click. Any suggestions? – Pearl Apr 30 '19 at 07:44
  • So you added a 2nd listener with `if(!e.isDoubleClick())`? I didn't try it out but I would say this is working as intended. A double click consists of a single click and a following second click. So I think it is okay that both click listeners are triggered. How should the code know that the first click is no "real" click but just a part of double click? There would have to be a mechanism which waits after a click and looks if a second click follows.. I think you can't make it work that way. Maybe you should move the "open info popup" logic to a button instead of opening it on "single click". – codinghaus Apr 30 '19 at 10:27
  • Thanks for your suggestion @codinghaus – Pearl Apr 30 '19 at 10:47