Couldn't capture double click event using vaadin 7

て烟熏妆下的殇ゞ 提交于 2020-02-04 04:51:25

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/55903127/couldnt-capture-double-click-event-using-vaadin-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!