View RSS Feed

My Java Tips

SWT Dialog

Rate this Entry
by , 11-10-2011 at 05:57 PM (1070 Views)
Dialog class is the abstract superclass of the classes that represent the built in platform dialogs. A Dialog typically contains other widgets that are not accessible. A Dialog is not a Widget.

This class can also be used as the abstract superclass for user-designed dialogs. Such dialogs usually consist of a Shell with child widgets. The basic template for a user-defined dialog typically looks something like this:

Java Code:
 import org.eclipse.swt.*;
 import org.eclipse.swt.widgets.*;
 import org.eclipse.swt.layout.*;

 public class SWTDialog extends Dialog {
	Object result;

	public SWTDialog (Shell parent, int style) {
		super (parent, style);
	public SWTDialog (Shell parent) {
		this (parent, 0); // your default style bits go here (not the Shell's style bits)
	public Object open () {
		Shell parent = getParent();
		Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		// Your code goes here (widget creation, set result, etc).;
		Display display = parent.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) display.sleep();
		return result;
Please note that the modality styles supported by this class must be treated as HINTs, because not all are supported by every subclass on every platform. If a modality style is not supported, it is "upgraded" to a more restrictive modality style that is supported. For example, if PRIMARY_MODAL is not supported by a particular dialog, it would be upgraded to APPLICATION_MODAL. In addition, as is the case for shells, the window manager for the desktop on which the instance is visible has ultimate control over the appearance and behavior of the instance, including its modality.

The styles supported by Dialog are: APPLICATION_MODAL, PRIMARY_MODAL, SYSTEM_MODAL. Please note that only one of the styles APPLICATION_MODAL, PRIMARY_MODAL, and SYSTEM_MODAL may be specified.

The following are the important constructors of Dialog class.

Dialog(Shell parent)
Constructs a new instance of this class given only its parent.

Dialog(Shell parent, int style)
Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

The following are the important methods of Dialog class.

protected void checkSubclass()
Checks that this class can be subclassed.

Shell getParent()
Returns the receiver's parent, which must be a Shell or null.

int getStyle()
Returns the receiver's style information.

String getText()
Returns the receiver's text, which is the string that the window manager will typically display as the receiver's title.

void setText(String string)
Sets the receiver's text, which is the string that the window manager will typically display as the receiver's title, to the argument, which must not be null.

Submit "SWT Dialog" to Facebook Submit "SWT Dialog" to Digg Submit "SWT Dialog" to Submit "SWT Dialog" to StumbleUpon Submit "SWT Dialog" to Google

Tags: dialog, subclass, swt Add / Edit Tags
SWT / JFace