Trigger button on ENTER inside a TextBox?

252 views
Skip to first unread message

membersound

unread,
Mar 13, 2013, 6:01:59 PM3/13/13
to google-we...@googlegroups.com
Hi,

I have a popup with a bunch of TextBox and TextArea fields. Also 3 Buttons like Save, Cancel, Reset.
For the TextBoxes I would like to trigger the Save action if ENTER is hit while inside a TextBox (not for the TextAreas).

Would I have to register a KeyDownHandler for all the TextBoxes (which would be lots of boilerplate code)? Or is there anything native in GWT catching ENTER key in TextBoxes?

Jens

unread,
Mar 13, 2013, 6:56:48 PM3/13/13
to google-we...@googlegroups.com
Do you use UiBinder for the DialogBox content widget? If so you can add more than one widget to @UiHandler:

@UiHandler({"box1", "box2", "box3"})
void maybeTriggerSave(KeyUpEvent event) {
  //check for ENTER and trigger save
}

Without UiBinder the above is equivalent to:

KeyUpHandler maybeTriggerSaveOnKeyUp = new KeyUpHanlder() { ...... };
box1.addKeyUpHandler(maybeTriggerSaveOnKeyUp);
box2.addKeyUpHandler(...)
....

If you instantiate the TextBoxes yourself you could replace "new TextBox()" with "createTextBox()" and add the key handler inside that method.

Alternatively you could listen for Key events on your content widget (addDomHandler()) of your DialogBox and then figure out if the event source was a TextBox and not a TextArea.

-- J.

K vfdsdfbsdb

unread,
Mar 13, 2013, 7:08:38 PM3/13/13
to google-we...@googlegroups.com

sure you must add keyListener for all TextBox .
But if you create one method,it 's smart.

example for

private void addListeners(List<TextBox> textboxs ,Listener listener){
for(int i=0;i<textboxs.size;i++){
     textboxs.get(i).addKeyListener(listener);
}
}

2013/03/14 7:02 "membersound" <kodyr...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Kody

unread,
Mar 14, 2013, 5:25:36 AM3/14/13
to google-we...@googlegroups.com
Thanks Jens. Yes I use uibinder, did not know I can register more than one widget to the same handler method. Will go for this approach.


2013/3/14 K vfdsdfbsdb <kazu...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "Google Web Toolkit" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-web-toolkit/BmA4uaw5HEM/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to google-web-tool...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages