2014-01-27 18 views
0

我正在尋找一種方法來在VerticalLayout或其他方式中使用回車鍵傳遞字段。在vaadin書中有一個Shortcut和Handler監聽器的例子,但我不知道如何實現它。使用回車鍵傳遞字段?

我正在試着這個。

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 
    } 
} 

}

什麼想法?

+0

如何使用,而不是輸入選項卡更改文本字段的重點是什麼? – FernandoPaiva

回答

1

嘗試這種方式與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); 

變化重點用輸入,而不是標籤:

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); 
1

沒有提供訪問器的接口,它可以讓您找到當前關注的組件。通過接口com.vaadin.event.FieldEvents.FocusListenercom.vaadin.event.FieldEvents.BlurListener可以獲取某些(但不是全部)現場組件的焦點信息。

您可以爲所有可能的字段添加一個FocusListener並記住每次調用變量中的當前字段。 (問題:並非所有字段都提供FocusListener。)然後,當按下ENTER時,根據當前的焦點字段(記住變量)聚焦下一個分量(記住變量),這個分量必須被聚焦(藉助簡單List,LinkedList,Map,開關等等)。爲了讓它更好,請添加BlurListener以瞭解何時不關注下一個字段。

希望有所幫助。