customJfaceDialog

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

	}

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *