本文介绍了无法使用Vaadin 7捕获双击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Vaadin框架.我正在尝试捕获项目单击侦听器的双击事件.但是它没有按预期工作.请参考下面的代码,

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.

推荐答案

中提到的那样在Vaadin网格上,问题是setEditorEnabled(true),因为它阻止了DoubleClick-Event的触发(因为网格上的双击事件似乎是Vaadin在内部使编辑器可见的触发器).

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. 可以双击并添加对doubleClick有反应的监听器
  2. 已在网格上启用编辑器

诀窍是首先禁用编辑器(默认情况下处于禁用状态),然后在ItemClickListener(如果为e.isDoubleClick())中自行"启用编辑器.

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()).

然后,您必须使用扩展Grid并覆盖方法doCancelEditor()的类.在此方法内部(单击时单击取消按钮,并在提交后单击时单击,将调用此方法),然后在按下取消"和/或保存"按钮之后再次禁用编辑器.

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:

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

这篇关于无法使用Vaadin 7捕获双击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 09:01