Friday 20 January 2017

jFace: GridLayoutFactory

jFace provides GridLayoutFactory, it is used to create and initialize grid layouts.

Ex:
GridLayoutFactory.swtDefaults().numColumns(2).applyTo(shell);
GridDataFactory.swtDefaults().grab(true, true).applyTo(shell);

Can I change the properties of GridLayoutFactory?
Yes you can change the properties, but remember changing a property of the factory will affect future layouts created by the factory, but has no effect on layouts that have already been created.

Following is the complete working application.
package test;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class TestGridLayout {

 private static void addWidgetsToShell(Display display, Shell shell) {

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

  GridLayoutFactory.swtDefaults().numColumns(2).applyTo(shell);
  GridDataFactory.swtDefaults().grab(true, true).applyTo(shell);

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

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

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

  Button button = new Button(shell, 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);

  shell.open();

 }

 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);
  shell.setSize(700, 200);
  shell.setText("GridLayout Demo");
  addWidgetsToShell(display, shell);

  /*
   * 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