Wednesday 18 January 2017

jFace: Creating Custom Dialogs

By extending Dialog class, you can create custom dialogs. Following are some of the useful methods, you need to override while creating custom dialogs.

Method
Description
protected void okPressed()
Notifies that the ok button of this dialog has been pressed.
protected Control createDialogArea(Composite parent)
Creates and returns the contents of the upper part of this dialog (above the button bar). The returned control's layout data must be an instance of GridData. This method must not modify the parent's layout.
protected void configureShell(Shell newShell)
Configures the given shell in preparation for opening this window in it.
protected Point getInitialSize()
Returns the initial size to use for the shell.

package test;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
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.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class CustomDialog extends Dialog {

 public String name;
 public String age;
 public String gender;
 public boolean employeedInSixMonths;
 private Text text1 = null;
 private Text text2 = null;
 private Text text3 = null;
 private Button button = null;

 public CustomDialog(Shell parentShell) {
  super(parentShell);
 }

 @Override
 protected void okPressed() {
  // Do operations if you want, when ok button pressed
  name = text1.getText();
  age = text2.getText();
  gender = text3.getText();
  employeedInSixMonths = button.getSelection();

  super.okPressed();
 }

 @Override
 protected Control createDialogArea(Composite parent) {
  Composite composite = (Composite) super.createDialogArea(parent);

  GridLayout gridLayout = new GridLayout();
  gridLayout.numColumns = 2;
  composite.setLayout(gridLayout);

  Label label1 = new Label(composite, SWT.NONE);
  label1.setText("Name:");
  text1 = new Text(composite, SWT.BORDER);

  Label label2 = new Label(composite, SWT.NONE);
  label2.setText("Age:");
  text2 = new Text(composite, SWT.BORDER);

  Label label3 = new Label(composite, SWT.NONE);
  label3.setText("Gender:");
  text3 = new Text(composite, SWT.BORDER);

  button = new Button(composite, SWT.CHECK);
  button.setText("Have you been employed in the past six months?");

  GridData gridData1 = new GridData();
  GridData gridData2 = new GridData();
  GridData gridData3 = new GridData();

  gridData1.widthHint = 60;
  gridData2.widthHint = 60;
  gridData3.widthHint = 60;

  label1.setLayoutData(gridData1);
  label2.setLayoutData(gridData2);
  label3.setLayoutData(gridData3);

  GridData gridData4 = new GridData(GridData.FILL_HORIZONTAL);
  GridData gridData5 = new GridData(GridData.FILL_HORIZONTAL);
  GridData gridData6 = new GridData(GridData.FILL_HORIZONTAL);

  text1.setLayoutData(gridData4);
  text2.setLayoutData(gridData5);
  text3.setLayoutData(gridData6);

  GridData gridData7 = new GridData();
  gridData7.horizontalSpan = 2;
  button.setLayoutData(gridData7);

  return composite;
 }

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

 @Override
 protected Point getInitialSize() {
  return new Point(450, 300);
 }

 @Override
 public String toString() {
  return "CustomDialog [name=" + name + ", age=" + age + ", gender=" + gender + ", employeedInSixMonths="
    + employeedInSixMonths + "]";
 }

}

package test;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

 public static void main(String[] args) {

  /* Instantiate Display object, it represents SWT session */
  Display display = new Display();

  /*
   * Define Shell, it represent a window, You can add more than one shell
   * to Display
   */
  Shell shell = new Shell(display);

  /* Open shell window */
  shell.open();

  Dialog dialog = new CustomDialog(Display.getDefault().getActiveShell());
  int pressedButton = dialog.open();

  if (pressedButton == 0) {
   System.out.println("Ok button pressed");
   System.out.println(dialog);
  }

  if (pressedButton == 1) {
   System.out.println("Cancel button pressed");
  }

  /*
   * Run the event dispatching loop until an exit condition occurs, which
   * is typically when the main shell window is closed by the user.
   */

  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }

  /* Dispose the display */
  display.dispose();
 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment