PDA

View Full Version : Simulate button pressed for a Dialog



listirer
18 Feb 2012, 3:20 AM
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...



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;
}

sven
18 Feb 2012, 5:11 AM
Why would you like to simulate it? Cannot you use

box.getDialog().hide(okButton);

listirer
18 Feb 2012, 7:18 AM
Thanks, It works!