Question

I'm looking for a way to pass fields with enter key in VerticalLayout or others. In vaadin book there an example with Shortcut and Handler listeners but I don't know how to implement that.

I'm trying this.

public class MyWindow extends Window implements Handler{
private Action action_enter; //pass fields with enter
private Action action_esc;
private TextField name, lastName;

public MyWindow(){
    super("this window is opened");
    VerticalLayout vLayout = new VerticalLayout();
    setContent(vLayout);

    center();
    setModal(true);
    setClosable(false);
    setDraggable(false);
    setResizable(false);    

    //actions
    action_enter = new ShortcutAction("Enter key", ShortcutAction.KeyCode.ENTER, null);
    action_esc = new ShortcutAction("Esc key", ShortcutAction.KeyCode.ESCAPE, null);
    addActionHandler(this);

    //fields
    name = new TextField("Name");
    lastName = new TextField("Last name");

    name.focus();
    vLayout.addComponent(name);
    vLayout.addComponent(lastName);     
}


@Override
public Action[] getActions(Object target, Object sender) {      
    return new Action[] { action_enter, action_esc };
}

@Override
public void handleAction(Action action, Object sender, Object target) {

    /** close window with esc key */
    if(action == action_esc){
        close();
    }   

    /** pass fields with enter key */
    if(action == action_enter){
        //here pass fields with enter key 
    }
}

}

any idea ?

Was it helpful?

Solution

try this way with ShortcutListener:

    ShortcutListener skEnterListener = new ShortcutListener("Enter", ShortcutAction.KeyCode.ENTER, null){

        @Override
        public void handleAction(Object sender, Object target) {

            if (target instanceof VerticalLayout) { // VerticalLayout or other
                 // sending fileds here
            }
        }
    };

    addShortcutListener(skEnterListener);

change focus of TextField using Enter instead Tab:

    final TextField tf1 = new TextField("tf1");
    tf1.setId("tf1");

    final TextField tf2 = new TextField("tf2");
    tf2.setId("tf2");


    ShortcutListener skEnterListener = new ShortcutListener("Enter", ShortcutAction.KeyCode.ENTER, null){

        @Override
        public void handleAction(Object sender, Object target) {

             if (target instanceof TextField) {

                 TextField field = (TextField) target;

                 if ("tf1".equals(field.getId())) {
                     tf2.focus();
                 }

                 if ("tf2".equals(field.getId())) {
                     tf1.focus();
                 }
             }
        }
    };

    addShortcutListener(skEnterListener);

OTHER TIPS

There is no interface which does provide an accessor that would allow you finding out the currently focused component. Focus information can be acquired for some (but not all) field components through the com.vaadin.event.FieldEvents.FocusListener and com.vaadin.event.FieldEvents.BlurListener interfaces.

You could add for all possible fields a FocusListener and remember every time it's invoked, the current field in a variable. (Problem: not all fields provide a FocusListener.) Then when ENTER is pressed focus the next component according to the current focused field (remember the variable) that has to be focused (with the help of a simple List, LinkedList, Map, switch-case and so forth). To make it even better add a BlurListener as well to know when not to focus the next field.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top