KeyPressed events within a JFace Dialog
If you have a widget with a key listener, you should detect key strokes through the keyReleased event rather than the keyPressed event.
This is critical if you want to be able to detect the [Enter] key (SWT.CR or SWT.KEYPAD_CR). When you press one of these enter keys, the KeyPressed event is processed by the dialog buttons and it will NOT be cascaded to your widget. Listen for KeyReleased events as shown in the next code block.
protected Control createDialogArea(Composite parent) {
...
...
filterText = new Text(controlComposite, SWT.BORDER);
filterText.addKeyListener(new KeyAdapter() {
// this will fail for SWT.CR and SWT.KEYPAD_CR; it **will** work fine for other key codes.
public void keyPressed(final KeyEvent e) {
int code = e.keyCode;
if(code == SWT.CR || code == SWT.KEYPAD_CR ){
// You will never get here
MessageDialog.openInformation(null, "Enter", "Enter code detected");
}
}
// this will work
public void keyReleased(final KeyEvent e) {
int code = e.keyCode;
if(code == SWT.CR || code == SWT.KEYPAD_CR || code == SWT.ARROW_DOWN){
MessageDialog.openInformation(null, "keyReleased: Enter or down", "Code is enter or down");
}
}
});
...
...
}
Consuming a Dialog
In the following example, NewWorkflowDialog extends org.eclipse.jface.dialogs.Dialog
Shell shell = getSite().getShell(); // get shell from ViewPart
NewWorkflowDialog dlg = new NewWorkflowDialog(shell);
int rc = dlg.open();
if(rc == Window.OK){
MessageDialog.openInformation(null, "Message", "User clicked OK!");
Thing thing = dlg.getThing();
process(thing);
}
Event Sequence
A class that extends the jface Dialog class will go through the following load sequence:
- constructor
- setter for data object if called immediately after the Dialog is instantiated.
- configureShell method
- createDialogArea method… don’t attempt to access controls in the previous methods.
- createButtonsForButtonBar method… SWT Designer may use this method to call initDatabinding().
Button Actions
To control behavior after a button in the button bar is pressed, override: okPressed, cancelPressed, or buttonPressed.
MessageDialog
MessageDialog.openInformation(null, "Message", "User clicked OK!");