import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.IntegerBox;
public class IntegerTextBox extends IntegerBox {
public IntegerTextBox() {
super();
}
public void initialize() {
addKeyDownHandler(new KeyDownHandler() {
public void onKeyDown(KeyDownEvent event) {
int keyCode = event.getNativeKeyCode();
switch (keyCode) {
case KeyCodes.KEY_LEFT:
case KeyCodes.KEY_RIGHT:
case KeyCodes.KEY_DELETE:
case KeyCodes.KEY_BACKSPACE:
case KeyCodes.KEY_ENTER:
case KeyCodes.KEY_ESCAPE:
case KeyCodes.KEY_TAB:
return;
}
String parsedInput = parseNumberKey(keyCode);
if (parsedInput.length() > 0) {
return;
}
else {
cancelKey();
}
}
});
}
/**
* Varifies that the key pressed is either a number key or a numeric pad key. Converts numeric pad keys to the string
* representation of their number values.
*
* @param keyCode - the key code.
* @return the key's number representation as a string.
*/
@SuppressWarnings("nls")
private String parseNumberKey(int keyCode) {
String result = new String();
switch (keyCode) {
case KeyCodes.KEY_ZERO:
case KeyCodes.KEY_ONE:
case KeyCodes.KEY_TWO:
case KeyCodes.KEY_THREE:
case KeyCodes.KEY_FOUR:
case KeyCodes.KEY_FIVE:
case KeyCodes.KEY_SIX:
case KeyCodes.KEY_SEVEN:
case KeyCodes.KEY_EIGHT:
case KeyCodes.KEY_NINE:
return result = String.valueOf((char) keyCode);
case KeyCodes.KEY_NUM_ZERO:
return result = "0";
case KeyCodes.KEY_NUM_ONE:
return result = "1";
case KeyCodes.KEY_NUM_TWO:
return result = "2";
case KeyCodes.KEY_NUM_THREE:
return result = "3";
case KeyCodes.KEY_NUM_FOUR:
return result = "4";
case KeyCodes.KEY_NUM_FIVE:
return result = "5";
case KeyCodes.KEY_NUM_SIX:
return result = "6";
case KeyCodes.KEY_NUM_SEVEN:
return result = "7";
case KeyCodes.KEY_NUM_EIGHT:
return result = "8";
case KeyCodes.KEY_NUM_NINE:
return result = "9";
}
return result;
}
}
I'm running into the same issue but agree that IntegerBox should be a more general solution. Although this post is old I believe it is still relevant. I'm attaching an IntegerTextBox class I created which subclasses IntegerBox. This class uses a KeyDownHandler to verify when a number key or a numeric pad key is pressed. It also allows required keys for operation of the text box, such as backspace, delete, left and right arrow keys, etc and cancels other keys. You could add some CSS3 trickery or some other form of visual cue to warn the user if unsupported keys are pressed.
-- Best,
- Julio
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-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment