问题
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)
oraddCommand(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