简体   繁体   English

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

[英]How to add Keylistener to ListSelect in Vaadin?

I have a vaadin ListSelect component on which I would like to remove items if the key DEL is used.我有一个 vaadin ListSelect组件,如果使用DEL键,我想在该组件上删除项目。

All I found was the ShortcutListener but if I add the following listener, the DEL key does not work in the rest of my application (eg deleting text in a TextField ):我发现的只是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
        }
    });

How should I implement a listener for the DEL key?我应该如何实现DEL键的侦听器?

EDIT: Tried to use a wrapper Panel as suggested in comments, but it still doesn't work.编辑:尝试按照评论中的建议使用包装Panel ,但它仍然不起作用。 Here my current code:这是我当前的代码:

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

The form is a GridLayout , the parent of form is a Panel . form是一个GridLayoutform的父级是一个Panel This panel is part of a TabSheet .此面板是TabSheet一部分。 I'm using Vaadin Version 7.7.1.我正在使用 Vaadin 7.7.1 版。

Looking at the sources (currently line 110) , it seems that the action is delegated to the containing window...查看源代码(当前第 110 行) ,似乎该操作已委托给包含窗口...

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

... or parent container at least, because based on this question Select-all shortcut (Ctrl-A) in Vaadin Table? ... 或至少父容器,因为基于这个问题Select-all 快捷方式 (Ctrl-A) in Vaadin Table? you can work around this issue.你可以解决这个问题。 If you wrap the list select in a panel and add the short-cut listener to the panel instead, it works as expected:如果您将列表选择包装在面板中并将快捷方式侦听器添加到面板中,它会按预期工作:

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

Actual output:实际输出:

vaadin 删除快捷键监听器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM