Vaadin 7: Detect Enter Key in a TextField

Vaadin 7: Detect Enter Key in a TextField

This can be easily achieved just by adding a shortcutlistener whenever the TextField gets the focus and remove it when it blurs. From an IOC perspective wrapping the TextField would most probably result in less rework than extending the TextField:

For example:

TextField myTextField = new TextField("A text field");
myTextField.setImmediate(true);
OnEnterKeyHandler onEnterHandler=new OnEnterKeyHandler(){
            @Override
            public void onEnterKeyPressed() {
                Notification.show("Voight Kampff Test",
                    Notification.Type.HUMANIZED_MESSAGE);
            }
        };
onEnterHandler.installOn(myTextField);


-------------------------------------------------------------------

And the code for OnEnterKeyHandler is:


import com.vaadin.event.FieldEvents;
import com.vaadin.event.ShortcutAction;
import com.vaadin.event.ShortcutListener;
import com.vaadin.ui.TextField;


public abstract class OnEnterKeyHandler {

     final ShortcutListener enterShortCut = new ShortcutListener(
                "EnterOnTextAreaShorcut", ShortcutAction.KeyCode.ENTER, null) {
                    @Override
                    public void handleAction(Object sender, Object target) {
                        onEnterKeyPressed();
                    }
                };

     public void installOn(final TextField component)
     {
        component.addFocusListener(
                new FieldEvents.FocusListener() {

                    @Override
                    public void focus(FieldEvents.FocusEvent event
                    ) {
                        component.addShortcutListener(enterShortCut);
                    }

                }
        );

        component.addBlurListener(
                new FieldEvents.BlurListener() {

                    @Override
                    public void blur(FieldEvents.BlurEvent event
                    ) {
                        component.removeShortcutListener(enterShortCut);
                    }

                }
        );
     }

     public abstract void onEnterKeyPressed();

}

Enjoy at your own risk.

Comments

  1. Thank you guy! its 100% effective! Happy new Year.

    ReplyDelete
  2. Hi, Its working great in textFields, but I have one problem.
    Its not working properly on DateFields
    any thoughts?
    Thank you

    ReplyDelete
    Replies
    1. In theory it would be possible to make it work in every 'Component'. Just by replacing the 'TextField' parameter on [installOn(final TextField component)] by [installOn(final Component component)]. I have used that approach and it does work, please, let me know if it does when DateFields are used. Thank you.

      Delete
  3. Thanks, here is my take on the solution with Java 8:

    import java.util.function.BiConsumer;

    import com.vaadin.event.ShortcutAction;
    import com.vaadin.event.ShortcutListener;
    import com.vaadin.ui.AbstractTextField;

    public abstract class AddHandler {
    public static void onEnter(AbstractTextField component, BiConsumer doOnEnter){
    ShortcutListener enterShortCut;
    enterShortCut = new ShortcutListener("EnterOnTextAreaShorcut", ShortcutAction.KeyCode.ENTER, null) {
    @Override
    public void handleAction(Object sender, Object target) {
    doOnEnter.accept(sender, target);
    }
    };
    component.addFocusListener(event -> component.addShortcutListener(enterShortCut));
    component.addBlurListener(event -> component.removeShortcutListener(enterShortCut));
    }
    }
    -----------------------------------
    Usage:
    TextField textField = new TextField();
    AddHandler.onEnter(textField, (Object sender, Object target)->{
    System.out.println("Works");
    });

    ReplyDelete
  4. Thank you for this. Work like a charm.

    ReplyDelete
  5. Works like a charm, thank you!

    ReplyDelete

Post a Comment

Popular posts from this blog

Qt Signals and Slots, Connecting and Disconnecting

JAVA JPA WITH HIBERNATE AND H2 Tutorial