5
TextField f = new TextField();
Button b = new Button("Save");
b.setClickShortcut(KeyCode.ENTER); // For quick saving from text field itself

TextArea longText = new TextArea(); // "Enter" is garbled here

使快捷方式仅在来自文本字段中有效吗?

4

3 回答 3

10

使用 focus 和 blur listeners 移除和添加快捷键:

    f.addFocusListener(new FocusListener() {
        @Override
        public void focus(FocusEvent event) {
            b.setClickShortcut(KeyCode.ENTER);
        }
    });
    f.addBlurListener(new BlurListener() {
        @Override
        public void blur(BlurEvent event) {
            b.removeClickShortcut();
        }
    });
于 2012-05-30T19:05:40.957 回答
2

较新版本的 Vaadin 需要以下代码,因为 addListener() 现在已弃用。

    f.addFocusListener(new FocusListener() {

        private static final long serialVersionUID = -6733373447805994139L;

        @Override
        public void focus(FocusEvent event) {
            b.setClickShortcut(KeyCode.ENTER);
        }
    });

    f.addBlurListener(new BlurListener() {

        private static final long serialVersionUID = -3673311830300629513L;

        @Override
        public void blur(BlurEvent event) {
            b.removeClickShortcut();
        }
    });
于 2014-02-09T09:15:18.940 回答
1

谈到 Vaadin 14,我一直在寻找答案,对我来说,这很好用

  Button search = new Button("Search");
  search.addClickShortcut(Key.ENTER);
于 2020-07-07T07:45:21.183 回答