问题
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:
- The possibility to doubleClick and add a Listener that reacts on doubleClicks
- 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