Wednesday, September 10, 2008

If you want to add a custom dialog to a JFace application the easiest way is to override the JFace Dialog class as follows:

public class MyDialog extends Dialog {
/**
* @param parentShell
*/
public MyDialog(Shell parentShell) {
super(parentShell);

//forces open method to block until window is closed.
setBlockOnOpen(true);

}

@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID,
IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID,
IDialogConstants.CANCEL_LABEL, false);

}


@Override
protected Control createDialogArea(Composite parent) {

Composite container = (Composite) super.createDialogArea(parent);

Label nameLabel = new Label(container,SWT.NONE);
nameLabel.setText("Name:");
Text nameText = new Text(container, SWT.BORDER);

return container;

}
}
The way you call it within any Action is as follows:
MyDialog d = new MyDialog(Display.getCurrent().getActiveShell());
int status = d.open();
For this example the open method will return OK or CANCEL. It is user-definable by overriding the getReturnCode() method on Dialog.

Make the Dialog resizable
By default it isnt, You must call this in the Dialog constructor:
Dialog.setShellStyle(SWT.RESIZE)

Change the Dialog title
You need to override the Window.configureShell method as follows

protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("My Dialog Title");
}

0 comments:

Post a Comment