如何在 Vaadin 中将 Keylistener 添加到 ListSelect?

How to add Keylistener to ListSelect in Vaadin?

我有一个 vaadin ListSelect 组件,如果使用 DEL 键,我想在该组件上删除项目。

我发现的只是 ShortcutListener 但如果我添加以下侦听器,DEL 键在我的应用程序的其余部分不起作用(例如删除 TextField 中的文本):

listSelect.addShortcutListener(new ShortcutListener("", KeyCode.DELETE, null) {
        private static final long serialVersionUID = 4703134138899283799L;

        @Override
        public void handleAction(Object sender, Object target) {
            // handle delete
        }
    });

我应该如何实现 DEL 键的侦听器?

编辑:尝试按照评论中的建议使用包装器 Panel,但它仍然不起作用。这是我当前的代码:

listSelect = new ListSelect(null);
listSelect.setWidth(100, Unit.PERCENTAGE);
listSelect.setHeight(82, Unit.PIXELS);
listSelect.setMultiSelect(true);
listSelect.setNullSelectionAllowed(false);

listSelect.setDescription("Löschen mit der DEL Taste");
listSelect.addShortcutListener(new ShortcutListener("", KeyCode.DELETE, null) {
    private static final long serialVersionUID = 4703134138899283799L;

    @Override
    public void handleAction(Object sender, Object target) {
        // handle delete
    }
});
Panel wrapperPanel = new Panel(listSelect);
form.addComponent(wrapperPanel);

formGridLayoutform 的父级是 Panel。此面板是 TabSheet 的一部分。我正在使用 Vaadin 版本 7.7.1。

查看 sources (currently line 110),似乎该操作已委托给包含 window...

/**
 * Keeps track of the Actions added to this component; the actual
 * handling/notifying is delegated, usually to the containing window.
 */
private ConnectorActionManager actionManager;

...或至少是父容器,因为基于这个问题 Select-all shortcut (Ctrl-A) in Vaadin Table? 您可以解决此问题。如果将列表 select 包装在面板中并将快捷方式侦听器添加到面板中,它会按预期工作:

public class MyListSelectComponent extends VerticalLayout {

    public MyListSelectComponent() {
        ListSelect list = new ListSelect("Press DEL to remove items");
        TextField input = new TextField("Focus this input and press DEL to delete some text");
        input.setValue("This is some very long text, or not...");

        for (int i = 0; i < 10; i++) {
            list.addItem(i);
        }

        Panel panel = new Panel(list);
        panel.addShortcutListener(new ShortcutListener("", ShortcutAction.KeyCode.DELETE, null) {
            @Override
            public void handleAction(Object sender, Object target) {
                if (list.getValue() != null) {
                    list.removeItem(list.getValue());
                }
            }
        });

        addComponent(panel);
        addComponent(input);
    }
}

实际输出: