Reputation: 26860
How do you go about creating KeyPress filter that would enforce only digits as input in text field. Something like this here:
http://www.smartclient.com/smartgwt/showcase/#form_keypress_filter
As added anoyence is there a way of doing this in uiBinder xml file?
Upvotes: 0
Views: 2969
Reputation: 1876
The code to do that is described here.
In your uiBinder file, define your TextBox item.
<g:TextBox uifield='textBox'/>
In your class, you can either add the KeyPressHandler directly, like :
textBox.addKeyPressHandler(new KeyPressHandler() {
@Override
public void onKeyPress(KeyPressEvent event) {
if (!"0123456789".contains(String.valueOf(event.getCharCode()))) {
textBox.cancelKey();
}
}
});
Or you can use the @UiHandler annotation like so:
@UiHandler("testBox")
public void onKeyPress(KeyPressEvent event) {
if (!"0123456789".contains(String.valueOf(event.getCharCode()))) {
textBox.cancelKey();
}
}
Upvotes: 5