Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Both ENTER shortcut and TextArea in Vaadin

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?

like image 929
Vi. Avatar asked May 30 '12 16:05

Vi.


2 Answers

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();
        }
    });
like image 181
Henri Kerola Avatar answered Oct 15 '22 17:10

Henri Kerola


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();
        }
    });
like image 30
Mirko Seifert Avatar answered Oct 15 '22 18:10

Mirko Seifert