FormLayoutComplex.java Source code

Java tutorial

Introduction

Here is the source code for FormLayoutComplex.java

Source

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.Composite;
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();
    }
}