处理 GWT-Ext NumberField 中 ENTER 键的回调
我使用 GWT-Ext 库为 Web 应用程序构建 GUI。 我想处理 NumberField 内的 ENTER 按键。
应该是这样的:
final NumberField requiredHeight = new NumberField();
requiredHeight.setValue(125);
requiredHeight.setAllowBlank(false);
requiredHeight.setAllowNegative(false);
requiredHeight.setAllowDecimals(false);
KeyListener listener = new KeyListener() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==13)
addPortlet(requiredHeight.getValue().intValue());
}
};
requiredHeight.addKeyListener(listener);
但是代码没有做任何事情! 我做错了什么?一般来说,将这样的处理程序添加到字段的最佳方法是什么?
I use GWT-Ext library for building GUI for a webapp.
I'd like to process an ENTER key press inside of the NumberField.
It should be like this:
final NumberField requiredHeight = new NumberField();
requiredHeight.setValue(125);
requiredHeight.setAllowBlank(false);
requiredHeight.setAllowNegative(false);
requiredHeight.setAllowDecimals(false);
KeyListener listener = new KeyListener() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==13)
addPortlet(requiredHeight.getValue().intValue());
}
};
requiredHeight.addKeyListener(listener);
But the code doesn't do anything!
What am I doing wrong? Generally speaking, what is the best way to add such a handler to the field?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,终于解决了。 KeyListener 内部还有另一个回调函数 -
componentKeyPress
而不是keyPressed
。这是正确的代码:OK, solved that at last. There is another callback function inside of KeyListener -
componentKeyPress
instead ofkeyPressed
. This is the correct code:有些方法已被弃用,我在简单的登录表单上使用以下代码:
Some methods are deprecated, I use following code on my simple login form:
注册一个按键侦听器实际上会将侦听器注册到 3 个不同的事件:按键、按键和按键,其中两个对于您的情况来说是完全没有意义的。
更紧凑的解决方案是使用通用侦听器,如下所示:
(请原谅我使用匿名侦听器类,它更快但显然具有相同的效果)
Registering a key listener in fact registers the listener to 3 different events: key-up, key-down and key-press, two of which are completely pointless in your case.
A more compact solution would be the usage of a generic listener, as follows:
(pardon me for using an anonymous listener class, it is quicker yet evidently has the same effect)