02 - Working With Eclipse Standard Widget Toolkit: Page 3 of 3

SWT Layout Managers

SWT defines many different types of layouts which are helpful in positioning and sizing of child widgets in a composite. In the following SWT Layout Tutorial Next we will discuss four type of layouts available in SWT.

1. Fill Layout

Fill layout is the simplest layout. It is used to layout widgets in a single row or column forcing them to be of same size.

public class MyFillLayout {
  Display myDisplay = new Display();
  Shell myShell = new Shell(myDisplay);

     public MyFillLayout() {
     FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
     fillLayout.marginHeight = 5;
     fillLayout.marginWidth = 5;
     fillLayout.spacing = 1;

      myShell.setLayout(fillLayout);

      Button button1 = new Button(myShell, SWT.PUSH);
      button1.setText("button1");

       Button button2 = new Button(myShell, SWT.PUSH);
       button2.setText("button2");

       Button button3 = new Button(myShell, SWT.PUSH);
       button3.setText("button3");

        myShell.pack();
        myShell.open();

        while (!myShell.isDisposed()) {
        if (!myDisplay.readAndDispatch()) {
            myDisplay.sleep();
            }
        }

        myDisplay.dispose();
        }

        public static void main(String[] args) {
        new MyFillLayout();
  }
}

2. Row Layout

Row Layout provides more options as compared to fill layout, which provide more control Row Layout has a number of configuration fields which can be used to control placement of the widget. In addition, the height and width of each control in a RowLayout can be specified by setting a RowData object into the control using setLayoutData()

public class MyRowLayout {
   public static void main(String[] args) {
      Button myButton;
      Display myDisplay = new Display();
      Shell myShell = new Shell(myDisplay);
      myShell.setText("MyRowLayout");
      myShell.setBounds(120, 120, 420, 120);
      RowLayout myLayout = new RowLayout();
      myShell.setLayout(myLayout);
      myLayout.spacing = 15;
      myLayout.marginTop = 15;
      myLayout.marginRight = 15;
      myLayout.marginLeft = 15;
      myLayout.marginBottom = 15;
      for (int i = 1; i <= 10; i++) {
         myButton = new Button(myShell, SWT.PUSH);
         myButton.setText("" + i);
      }
      myShell.open();
      while (!myShell.isDisposed()) {
         if (!myDisplay.readAndDispatch()) myDisplay.sleep();
      }
      myDisplay.dispose();
   }
}

3. Grid Layout

SWT Gridlayout is to arrange children in a grid of rows and columns, we have many options to control the sizing of each child widget.

public class MyGridLayout {
   public static void main(String[] args) {

// Gridlayout swt image tutorial

      Display myDisplay = new Display();
      Shell myShell = new Shell(myDisplay);
      myShell.setText("My GridLayout");
      myShell.setBounds(120, 120, 220, 120);
      GridLayout myLayout = new GridLayout();
      myLayout.numColumns = 2;
      myShell.setLayout(myLayout);

      Label myLabel = new Label(myShell, SWT.LEFT);
      myLabel.setText("Please enter your age and birthdate");
      GridData gridData = new GridData();
      gridData.horizontalSpan = 2;
      myLabel.setLayoutData(gridData);

      myLabel = new Label(myShell, SWT.LEFT);
      myLabel.setText("Age:");

      Text myText = new Text(myShell, SWT.SINGLE | SWT.BORDER);
      gridData = new GridData();
      gridData.horizontalAlignment = GridData.FILL;
      gridData.grabExcessHorizontalSpace = true;
      myText.setLayoutData(gridData);

      myLabel = new Label(myShell, SWT.LEFT);
      myLabel.setText("BirthDate");
      myText = new Text(myShell, SWT.SINGLE | SWT.BORDER);
      gridData = new GridData();
      gridData.horizontalAlignment = GridData.FILL;
      gridData.grabExcessHorizontalSpace = true;
      myText.setLayoutData(gridData);

      myShell.open();
      while (!myShell.isDisposed()) {
         if (!myDisplay.readAndDispatch()) myDisplay.sleep();
      }
      myDisplay.dispose();
   }
}  

4. Form Layout

Instances of this class control the position and size of the children of a composite control by using FormAttachments to optionally configure the left, top, right and bottom edges of each child. It is one of the most powerful layout managers.

public class MyFormLayout {
   public static void main(String[] args) {

    Display myDisplay = new Display();
      final Shell myShell = new Shell(myDisplay);
      myShell.setText("My FormLayout");
      myShell.setBounds(120, 120, 240, 200);
      myShell.setLayout(new FormLayout());
      Button myCancelButton = new Button(myShell, SWT.PUSH);
      myCancelButton.setText("This is Cancel Button");
      FormData myFormData = new FormData();
      myFormData.right = new FormAttachment(100,-10);
      myFormData.bottom = new FormAttachment(100,-10);
      myCancelButton.setLayoutData(myFormData);
      Button myOKButton = new Button(myShell, SWT.PUSH);
      myOKButton.setText("This is OK Button");
      myFormData = new FormData();
      myFormData.right = new FormAttachment(myCancelButton,-10);
      myFormData.bottom = new FormAttachment(100,-10);
      myOKButton.setLayoutData(myFormData);
      Text myTextBox = new Text(myShell, SWT.MULTI | SWT.BORDER);
      myFormData = new FormData();
      myFormData.top = new FormAttachment(0,10);
      myFormData.bottom = new FormAttachment(
         myCancelButton,-10);
      myFormData.left = new FormAttachment(0,10);
      myFormData.right = new FormAttachment(100,-10);
      myTextBox.setLayoutData(myFormData);
      myShell.open();
      while (!myShell.isDisposed()) {
         if (!myDisplay.readAndDispatch()) myDisplay.sleep();
      }
      myDisplay.dispose();
   }

 

Like us on Facebook