I have crated a Dialog via MessageBox (prompt method) and added a KeyListener to the TextField.
How can I simulate the pressed of button OK?
The onButtonPressed method of Dialog is protected...
Code:
public static MessageBox promptMessage(String title, String message,
Listener<MessageBoxEvent> callback) {
final MessageBox box = MessageBox.prompt(title, message, callback);
final Button okButton = box.getDialog().getButtonById(Dialog.OK);
okButton.disable();
final TextField<String> textBox = box.getTextBox();
textBox.addKeyListener(new KeyListener() {
@Override
public void componentKeyPress(ComponentEvent event) {
if (okButton.isEnabled()
&& event.getKeyCode() == KeyCodes.KEY_ENTER) {
box.close();
}
}
});
box.addListener(Events.OnKeyUp, new Listener<MessageBoxEvent>() {
@Override
public void handleEvent(MessageBoxEvent be) {
String value = textBox.getValue();
if (value == null || value.trim().equals("")) {
okButton.disable();
} else {
okButton.enable();
}
}
});
return box;
}