Both ENTER shortcut and TextArea in Vaadin
Asked Answered
M

4

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

Hot to make the shortcut to work only in the from text field?

Morphogenesis answered 30/5, 2012 at 16:12 Comment(0)
B
10

Use focus and blur listeners to remove and add the shortcut key:

    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();
        }
    });
Bucaramanga answered 30/5, 2012 at 19:5 Comment(0)
M
2

Newer versions of Vaadin require the following code as addListener() is deprecated now.

    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();
        }
    });
Morganatic answered 9/2, 2014 at 9:15 Comment(0)
D
0

Talking in terms of Vaadin 14, I was looking for the answer and for me, this worked well

  Button search = new Button("Search");
  search.addClickShortcut(Key.ENTER);
Debatable answered 7/7, 2020 at 7:45 Comment(0)
N
0

As of Vaadin 23 (and probably for sometime before) the requirements have changed again.


private ShortcutRegistration primaryShortCut;

void customShortCutHandling()
    {
        myTextAreaField.addFocusListener((e) ->
            {
                System.out.println("disable");
                primaryShortCut = primaryButton.addClickShortcut(Key.ENTER);
            });

        myTextAreaField.addBlurListener((e) ->
            {
                System.out.println("enable");
                primaryShortCut.remove();
            });
    }
}

This code assumes that primaryShortCut was set when the form is created.

Number answered 17/4, 2022 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.