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

时间:2016-09-22 06:27:35

标签: java vaadin vaadin7

我有一个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。

1 个答案:

答案 0 :(得分:2)

查看sources (currently line 110),似乎将操作委托给包含窗口......

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

实际输出:

vaadin delete key shortcut listener