Java Tutorial/SWT/FormLayout

Материал из Java эксперт
Версия от 15:21, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Attaching a Control to Another Control

The side of the control can be attached to the adjacent side of the other control (default alignment), to the opposite side of the other control, or centered on the other control. Additionally, you can specify offsets.


Attaching a Control to a Position in the Parent Composite

To attach the control to a position in the parent composite, you need to define the position of a side of the control using a percentage value.



import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutAttachControl {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    FormData formData = new FormData();
    formData.left = new FormAttachment(50);
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    button1.setLayoutData(formData);
    shell.setSize(450, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Attaching to the Adjacent Side of Another Control

By default, the control is attached to the adjacent side of the other control.



import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutAttachAnotherControl {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("button2");
    FormData formData = new FormData();
    formData.left = new FormAttachment(button1);
    button2.setLayoutData(formData);
    Button button3 = new Button(shell, SWT.PUSH);
    button3.setText("button3");
    formData = new FormData();
    formData.top = new FormAttachment(button2, 10);
    button3.setLayoutData(formData);
    shell.setSize(450, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Attaching to the Specified Side of Another Control

The following constants are possible values for the alignment property:

  1. org.eclipse.swt.SWT.TOP: to the top side of the specified control
  2. org.eclipse.swt.SWT.TOP is for top and bottom alignments only.
  3. org.eclipse.swt.SWT.BOTTOM: to the bottom side of the specified control.
  4. org.eclipse.swt.SWT.BOTTOM is for top and bottom alignments only.
  5. org.eclipse.swt.SWT.LEFT: to the left side of the specified control.
  6. org.eclipse.swt.SWT.LEFT is for left and right alignments only.
  7. org.eclipse.swt.SWT.RIGHT: to the right side of the specified control.
  8. org.eclipse.swt.SWT.RIGHT is for left and right alignments only.
  9. org.eclipse.swt.SWT.CENTER: center the control on the specified control.
  10. org.eclipse.swt.SWT.DEFAULT: to the adjacent side of the specified control.



import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutAnotherControl {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("button2");
    FormData formData = new FormData();
    formData.left = new FormAttachment(button1);
    button2.setLayoutData(formData);
    Button button3 = new Button(shell, SWT.PUSH);
    button3.setText("button3");
    formData = new FormData();
    formData.top = new FormAttachment(button2, 10);
    formData.left = new FormAttachment(button2, 0, SWT.LEFT);
    button3.setLayoutData(formData);

    shell.setSize(450, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Attach the bottom button to the upper-left button and the window:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutComposite {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    Composite composite = new Composite(shell, SWT.NONE);
    
    GridLayout gridLayout = new GridLayout();
    gridLayout.marginHeight = 0;
    gridLayout.marginWidth = 0;
    composite.setLayout(gridLayout);
    Button two = new Button(composite, SWT.PUSH);
    two.setText("two");
    GridData gridData = new GridData(GridData.FILL_BOTH);
    two.setLayoutData(gridData);
    Button three = new Button(composite, SWT.PUSH);
    three.setText("three");
    gridData = new GridData(GridData.FILL_BOTH);
    three.setLayoutData(gridData);
    Button four = new Button(composite, SWT.PUSH);
    four.setText("four");
    gridData = new GridData(GridData.FILL_BOTH);
    four.setLayoutData(gridData);
    
    
    Button five = new Button(shell, SWT.PUSH);
    five.setText("five");
    FormData data = new FormData();
    data.top = new FormAttachment(two, 5);
    data.left = new FormAttachment(0, 5);
    data.bottom = new FormAttachment(100, -5);
    data.right = new FormAttachment(100, -5);
    five.setLayoutData(data);
    
    
    
    data = new FormData();
    data.top = new FormAttachment(0, 5);
    data.left = new FormAttachment(two, 5);
    data.bottom = new FormAttachment(50, -5);
    data.right = new FormAttachment(100, -5);
    composite.setLayoutData(data);    
    
    
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Center a control on another control

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutCenterAnotherControl {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("button222222222222222222222");
    FormData formData = new FormData();
    formData.left = new FormAttachment(button1);
    button2.setLayoutData(formData);
    Button button3 = new Button(shell, SWT.PUSH);
    button3.setText("button3");
    formData = new FormData();
    formData.top = new FormAttachment(button2, 10);
    formData.left = new FormAttachment(button2, 0, SWT.CENTER);
    button3.setLayoutData(formData);

    shell.setSize(450, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Center control both horizontally and vertically

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutCenterControlHori {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    FormData formData = new FormData();
    formData.left = new FormAttachment(20);
    formData.top = new FormAttachment(20);
    button1.setLayoutData(formData);
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("button number 2");
    formData = new FormData();
    formData.left = new FormAttachment(button1, 0, SWT.CENTER);
    formData.top = new FormAttachment(button1, 0, SWT.CENTER);
    button2.setLayoutData(formData);
    shell.setSize(450, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





FormData Objects and FormAttachment Objects

  1. FormData objects specify how controls should be laid out.
  2. FormData objects define the preferred size and the attachments for four sides of the control.
  3. FormAttachment objects control the position and size of the control.



import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutFormDataAttachment {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    shell.setSize(450, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





FormData: width and height

Set the preferred width and height of the control.



import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutFormDataWidthHeight {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    FormData formData = new FormData();
    formData.width = 100;
    formData.height = 200;
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    button1.setLayoutData(formData);
    shell.setSize(450, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





FormLayout: attach each other

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutAttachEachOther {
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setLayout(new FormLayout());
    
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    FormData formData = new FormData();
    formData.left = new FormAttachment(20);
    formData.top = new FormAttachment(20);
    button1.setLayoutData(formData);    
    
    
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("button number 2");
    
    formData = new FormData();
    formData.left = new FormAttachment(button1, 0, SWT.CENTER);
    formData.top = new FormAttachment(button1, 0, SWT.CENTER);
    button2.setLayoutData(formData);
    
    shell.pack();
    shell.open();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
}





FormLayout: attach with left and right margin

Specifies the size of the vertical margin in pixels. The default value is 0.



import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutLeftRight {
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setLayout(new FormLayout());
    
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    FormData formData = new FormData();
    formData.left = new FormAttachment(20);
    formData.top = new FormAttachment(20);
    button1.setLayoutData(formData);    
    
    shell.pack();
    shell.open();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
}





FormLayout Complex

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutComplex {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    FormLayout layout = new FormLayout();
    shell.setLayout(layout);
    Button one = new Button(shell, SWT.PUSH);
    one.setText("One");
    FormData data = new FormData();
    data.top = new FormAttachment(0, 5);
    data.left = new FormAttachment(0, 5);
    data.bottom = new FormAttachment(50, -5);
    data.right = new FormAttachment(50, -5);
    one.setLayoutData(data);
    Composite composite = new Composite(shell, SWT.NONE);
    GridLayout gridLayout = new GridLayout();
    gridLayout.marginHeight = 20;
    gridLayout.marginWidth = 20;
    composite.setLayout(gridLayout);
    Button two = new Button(composite, SWT.PUSH);
    two.setText("two");
    GridData gridData = new GridData(GridData.FILL_BOTH);
    two.setLayoutData(gridData);
    Button three = new Button(composite, SWT.PUSH);
    three.setText("three");
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    three.setLayoutData(gridData);
    Button four = new Button(composite, SWT.PUSH);
    four.setText("four");
    gridData = new GridData(GridData.FILL_BOTH);
    four.setLayoutData(gridData);
    data = new FormData();
    data.top = new FormAttachment(0, 15);
    data.left = new FormAttachment(one, 25);
    data.bottom = new FormAttachment(50, -15);
    data.right = new FormAttachment(100, -25);
    composite.setLayoutData(data);
    Button five = new Button(shell, SWT.PUSH);
    five.setText("five");
    data = new FormData();
    data.top = new FormAttachment(one, 5);
    data.left = new FormAttachment(0, 5);
    data.bottom = new FormAttachment(100, -5);
    data.right = new FormAttachment(100, -5);
    five.setLayoutData(data);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





FormLayout example snippet: center a label and single line text using a form layout

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * FormLayout example snippet: center a label and single line text using a form layout
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
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 FormLayoutSingleLine {
public static void main (String [] args) {
  Display display = new Display ();
  Shell shell = new Shell (display);
  Label label = new Label (shell, SWT.NONE | SWT.BORDER);
  label.setText ("Name");
  
  Text text = new Text (shell, SWT.NONE);
  FormLayout layout = new FormLayout ();
  layout.marginWidth = layout.marginHeight = 5;
  shell.setLayout (layout);
  FormData data = new FormData (200, SWT.DEFAULT);
  text.setLayoutData (data);
  data.left = new FormAttachment (label, 5);
  data.top = new FormAttachment (label, 0, SWT.CENTER);
  
  shell.open ();
  while (!shell.isDisposed ()) {
    if (!display.readAndDispatch ()) display.sleep ();
  }
  display.dispose ();
}
}





FormLayout example snippet: create a simple dialog using form layout

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * FormLayout example snippet: create a simple dialog using form layout
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class DialogLayoutDemo {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    Label label = new Label(shell, SWT.WRAP);
    label.setText("This is a long text string that will wrap when the dialog is resized.");
    List list = new List(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    list.setItems(new String[] { "Item 1", "Item 2" });
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("OK");
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Cancel");
    final int insetX = 4, insetY = 4;
    FormLayout formLayout = new FormLayout();
    formLayout.marginWidth = insetX;
    formLayout.marginHeight = insetY;
    shell.setLayout(formLayout);
    Point size = label.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
    final FormData labelData = new FormData(size.x, SWT.DEFAULT);
    labelData.left = new FormAttachment(0, 0);
    labelData.right = new FormAttachment(100, 0);
    label.setLayoutData(labelData);
    shell.addListener(SWT.Resize, new Listener() {
      public void handleEvent(Event e) {
        Rectangle rect = shell.getClientArea();
        labelData.width = rect.width - insetX * 2;
        shell.layout();
      }
    });
    FormData button2Data = new FormData();
    button2Data.right = new FormAttachment(100, -insetX);
    button2Data.bottom = new FormAttachment(100, 0);
    button2.setLayoutData(button2Data);
    FormData button1Data = new FormData();
    button1Data.right = new FormAttachment(button2, -insetX);
    button1Data.bottom = new FormAttachment(100, 0);
    button1.setLayoutData(button1Data);
    FormData listData = new FormData();
    listData.left = new FormAttachment(0, 0);
    listData.right = new FormAttachment(100, 0);
    listData.top = new FormAttachment(label, insetY);
    listData.bottom = new FormAttachment(button2, -insetY);
    list.setLayoutData(listData);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





FormLayout example snippet: create a simple OK/CANCEL dialog using form layout

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * FormLayout example snippet: create a simple OK/CANCEL dialog using form layout
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class DialogOKCancelFormLayout {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
    Label label = new Label(dialog, SWT.NONE);
    label.setText("Exit the application?");
    Button okButton = new Button(dialog, SWT.PUSH);
    okButton.setText("&OK");
    Button cancelButton = new Button(dialog, SWT.PUSH);
    cancelButton.setText("&Cancel");
    FormLayout form = new FormLayout();
    form.marginWidth = form.marginHeight = 8;
    dialog.setLayout(form);
    FormData okData = new FormData();
    okData.top = new FormAttachment(label, 8);
    okButton.setLayoutData(okData);
    FormData cancelData = new FormData();
    cancelData.left = new FormAttachment(okButton, 8);
    cancelData.top = new FormAttachment(okButton, 0, SWT.TOP);
    cancelButton.setLayoutData(cancelData);
    dialog.setDefaultButton(okButton);
    dialog.pack();
    dialog.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





FormLayout: spacing

Specifies the number of pixels between the edge of one control and the edge of its neighboring control. The default value is 0.


Specify attachments for all four sides of a control.

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutAttachFourSidesControl {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    FormData formData = new FormData();
    formData.left = new FormAttachment(50);
    formData.right = new FormAttachment(100);
    formData.top = new FormAttachment(50);
    formData.bottom = new FormAttachment(100);
    button1.setLayoutData(formData);
    shell.setSize(450, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Specify the offset of the control side from the attachment position

  1. Positive value: the control side is offset to the right of or below the attachment position.
  2. Negative value: the control side is offset to the left of or above the attachment position.



import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutControlOffsetAttachment {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    Point size = button1.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
    int offset = size.x / 2;
    FormData formData = new FormData();
    formData.left = new FormAttachment(50, -1 * offset);
    button1.setLayoutData(formData);

    shell.setSize(450, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Start with the upper-left button, attach the top and left edges to the window, offsetting by five pixels:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutFormAttachmentSetButton {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    FormLayout layout = new FormLayout();
    layout.marginHeight = 5;
    layout.marginWidth = 10;
    shell.setLayout(layout);
    Button one = new Button(shell, SWT.PUSH);
    one.setText("One");
    FormData data = new FormData();
    data.top = new FormAttachment(0, 5);
    data.left = new FormAttachment(0, 5);
    data.bottom = new FormAttachment(50, -5);
    data.right = new FormAttachment(50, -5);
    one.setLayoutData(data);    
    
    
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





To configure the attachment of each side

  1. top: Specifies the attachment of the top of the control.
  2. bottom: Specifies the attachment of the bottom of the control.
  3. left: Specifies the attachment of the left of the control.
  4. right: Specifies the attachment of the right of the control.

A FormAttachment object specifies the attachment of a specific side of a control. You can attach a side to a position in the parent composite, to the adjacent side of another control, to the opposite side of another control, or centered on another control.


To create the upper-right three buttons, you reason that you can put them all in a composite with a grid layout, and attach the composite to your first button:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutComposite {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    Composite composite = new Composite(shell, SWT.NONE);
    
    GridLayout gridLayout = new GridLayout();
    gridLayout.marginHeight = 0;
    gridLayout.marginWidth = 0;
    composite.setLayout(gridLayout);
    Button two = new Button(composite, SWT.PUSH);
    two.setText("two");
    GridData gridData = new GridData(GridData.FILL_BOTH);
    two.setLayoutData(gridData);
    Button three = new Button(composite, SWT.PUSH);
    three.setText("three");
    gridData = new GridData(GridData.FILL_BOTH);
    three.setLayoutData(gridData);
    Button four = new Button(composite, SWT.PUSH);
    four.setText("four");
    gridData = new GridData(GridData.FILL_BOTH);
    four.setLayoutData(gridData);
    
    FormData data = new FormData();
    data.top = new FormAttachment(0, 5);
    data.left = new FormAttachment(two, 5);
    data.bottom = new FormAttachment(50, -5);
    data.right = new FormAttachment(100, -5);
    composite.setLayoutData(data);    
    
    
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Use FormLayout to layout a dialog

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutDialogDemo {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    Label label = new Label(shell, SWT.WRAP);
    label.setText("Some text for your dialog.");
    List list = new List(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    list.setItems(new String[] { "Item 1", "Item2" });
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("Ok");
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Cancel");
    final int insetX = 4, insetY = 4;
    FormLayout formLayout = new FormLayout();
    formLayout.marginWidth = insetX;
    formLayout.marginHeight = insetY;
    shell.setLayout(formLayout);
    Point size = label.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
    final FormData labelData = new FormData(size.x, SWT.DEFAULT);
    labelData.left = new FormAttachment(0, 0);
    labelData.right = new FormAttachment(100, 0);
    label.setLayoutData(labelData);
    shell.addListener(SWT.Resize, new Listener() {
      public void handleEvent(Event e) {
        Rectangle rect = shell.getClientArea();
        labelData.width = rect.width - insetX * 2;
        shell.layout();
      }
    });
    FormData button2Data = new FormData();
    button2Data.right = new FormAttachment(100, -insetX);
    button2Data.bottom = new FormAttachment(100, 0);
    button2.setLayoutData(button2Data);
    FormData button1Data = new FormData();
    button1Data.right = new FormAttachment(button2, -insetX);
    button1Data.bottom = new FormAttachment(100, 0);
    button1.setLayoutData(button1Data);
    FormData listData = new FormData();
    listData.left = new FormAttachment(0, 0);
    listData.right = new FormAttachment(100, 0);
    listData.top = new FormAttachment(label, insetY);
    listData.bottom = new FormAttachment(button2, -insetY);
    list.setLayoutData(listData);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Using FormLayouts

  1. A FormLayout uses FormAttachment objects to configure the left, right, top, and bottom edge of a widget.
  2. A FormAttachment attaches a side of the control either to a position in the parent composite or to one of its sibling controls within the layout.


Without FormAttachment objects

Without FormAttachment objects, the control will be attached to the top-left edges of the parent composite by default.

Button will stack.



import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FormLayoutFormAttachment {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("button1");
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("button2");
    shell.setSize(450, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}