GXT KeyListener.componentKeyDown() immediately closes MessageBox.alert()

戏子无情 提交于 2019-12-24 11:50:11

问题


In GXT, MessageBox methods are asynchronous, meaning that the application does not "lock up" while the message box is displayed.

I using a KeyListener to process enter key presses in a form (to increase usability, i.e., allowing the form to be submitted by the enter key) and subsequently disabling the form fields while the application processes the user's credentials. If they are incorrect, I show a MessageBox.alert() and then re-enable the form fields. However, since alert() returns immediately, the form fields are immediately made available again, allowing the user to input data without closing the alert.

The solution is to use a callback in alert(); however, the enter keypress not only causes the form to submit, but also causes the alert to immediately dismiss (as if both the form and the message box are processing the enter key). How do I keep the alert box open until the user presses enter a second time or clicks the "Ok" button?


回答1:


The key is DeferredCommand provided by GWT:

This class allows you to execute code after all currently pending event handlers have completed, using the addCommand(Command) or addCommand(IncrementalCommand) methods. This is useful when you need to execute code outside of the context of the current stack.

if(!validate())
{
    DeferredCommand.addCommand(new Command() {
        public void execute() {
            MessageBox.alert("Error", "You must enter a username and password.", alertListener);
            return;
        }
    });
}


来源:https://stackoverflow.com/questions/691684/gxt-keylistener-componentkeydown-immediately-closes-messagebox-alert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!