Custom SWT Dialog

Custom SWT Dialogbox extending org.eclipse.swt.widgets.Dialog Class

Source :


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class EmployeeDialog extends Dialog {

	public EmployeeDialog(Shell parent) {
		super(parent);
	}

	private Group insGroup;

	public void open() {
		Shell shell = new Shell(getParent());
		shell.setText("Add Employee");

		draw(shell); // Contents of Dialog
		shell.pack();
		shell.open();

		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

	}

	private void draw(Shell shell) {

		shell.setLayout(new GridLayout(2, true));

		// ID

		Label lid = new Label(shell, SWT.HORIZONTAL | SWT.SHADOW_OUT);
		lid.setText("Employee ID");
		Text tid = new Text(shell, SWT.BORDER | SWT.LEFT);

		// Age

		Label lage = new Label(shell, SWT.HORIZONTAL | SWT.SHADOW_OUT);
		lage.setText("Age");
		Text tage = new Text(shell, SWT.BORDER | SWT.LEFT);

		// Salary

		Label lsalary = new Label(shell, SWT.HORIZONTAL | SWT.SHADOW_OUT);
		lsalary.setText("Salary");
		Text tsalary = new Text(shell, SWT.BORDER | SWT.LEFT);

		final Button checkButton = new Button(shell, SWT.CHECK);
		checkButton.setText("Is Insured ?");
		checkButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				if (checkButton.getSelection()) {
					insGroup.setVisible(true);

				} else {
					insGroup.setVisible(false);
				}
			}

		});

		// Insurance Group

		insGroup = new Group(shell, SWT.SHADOW_IN);
		insGroup.setText("Insurance Details");
		insGroup.setLayout(new GridLayout(1, true));
		Label lb = new Label(insGroup, SWT.HORIZONTAL | SWT.SHADOW_OUT);
		lb.setText("Description");
		Text details = new Text(insGroup, SWT.BORDER | SWT.MULTI);
		insGroup.setVisible(false);

	}

}

Custom JFace Dialog

Custom Dialogbox  extending org.eclipse.jface.dialogs.Dialog class

Source:

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;

public class JfaceEmployeeDialog extends Dialog {

	public JfaceEmployeeDialog(Shell parent) {
		super(parent);
	}

	@Override
	protected Control createDialogArea(Composite parent) {

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

		composite.getShell().setText("Add Employee");

		composite.setLayout(new GridLayout(1, true));

		draw(composite); // Contents of Dialog

		return composite;
	}

	private void draw(Composite composite) {

		// Employee Group

		Group empgroup = new Group(composite, SWT.SHADOW_IN);
		empgroup.setText("Employee Details");
		empgroup.setLayout(new GridLayout(3, true));

		// ID

		Label label = new Label(empgroup, SWT.HORIZONTAL);
		label.setText("Employee ID");
		Text tid = new Text(empgroup, SWT.FILL | SWT.BORDER);

		GridData textData = new GridData(SWT.FILL, SWT.FILL, true, true);
		textData.horizontalSpan = 2;
		textData.widthHint = 300;
		tid.setLayoutData(textData);

		// Name

		Label l_name = new Label(empgroup, SWT.HORIZONTAL);
		l_name.setText("Name");
		Text t_name = new Text(empgroup, SWT.FILL | SWT.BORDER);
		t_name.setLayoutData(textData);

		// Age

		Label l_age = new Label(empgroup, SWT.HORIZONTAL);
		l_age.setText("Age");
		Text t_age = new Text(empgroup, SWT.FILL | SWT.BORDER);
		t_age.setLayoutData(textData);

		Label lb = new Label(empgroup, SWT.HORIZONTAL);
		lb.setText("Is Insured");

		// CheckBox

		final Button checkBox = new Button(empgroup, SWT.CHECK);
		empgroup.setVisible(true);

		// Insurance Group

		final Group insgroup = new Group(composite, SWT.SHADOW_IN);
		insgroup.setText("Insurance Details");
		insgroup.setLayout(new GridLayout(3, true));

		// Amount

		Label l_amount = new Label(insgroup, SWT.HORIZONTAL);
		l_amount.setText("Amount");
		Text t_amount = new Text(insgroup, SWT.FILL | SWT.BORDER);
		t_amount.setLayoutData(textData);
		insgroup.setVisible(false);

		// Adding listener to check button

		checkBox.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				if (checkBox.getSelection()) {
					insgroup.setVisible(true);
				} else {
					insgroup.setVisible(false);
				}
			}
		});

	}

}