FormLayout: create a simple dialog using form layout : Layout « SWT JFace Eclipse « Java






FormLayout: create a simple dialog using form layout

FormLayout: create a simple dialog using form layout


/*
 * FormLayout example snippet: create a simple dialog using form layout
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#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 Snippet65 {

  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", "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.computeSize(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();
  }
}

           
       








Related examples in the same category

1.SWT Layout Example in OneSWT Layout Example in One
2.SWT GridLayout DemoSWT GridLayout Demo
3.SWT GridLayoutSWT GridLayout
4.Control Size LocationControl Size Location
5.SWT RowLayout SampleSWT RowLayout Sample
6.SWT FillLayout SampleSWT FillLayout Sample
7.SWT Form Layout SampleSWT Form Layout Sample
8.SWT GridLayout SampleSWT GridLayout Sample
9.GridLayout Sample Grab SpaceGridLayout Sample Grab Space
10.Grid Layout SpanGrid Layout Span
11.Layout ComponentsLayout Components
12.Layout TermsLayout Terms
13.StackLayout SampleStackLayout Sample
14.BorderLayout SampleBorderLayout Sample
15.FormLayoutsFormLayouts
16.FormLayout SimpleFormLayout Simple
17.A BorderLayoutA BorderLayout
18.FillLayout HorizontalFillLayout Horizontal
19.FillLayout VerticalFillLayout Vertical
20.FormLayout ComplexFormLayout Complex
21.FormLayout Form AttachmentFormLayout Form Attachment
22.FormLayoutFormLayout
23.GridLayout 2x2GridLayout 2x2
24.Grid Layout ComplexGrid Layout Complex
25.No LayoutNo Layout
26.Row Layout TestRow Layout Test
27.Stack Layout TestStack Layout Test
28.Row Layout HorizontalRow Layout Horizontal
29.FillLayout ExampleFillLayout Example
30.FormLayout ExampleFormLayout Example
31.GridLayout ExampleGridLayout Example
32.GridLayout Example 2GridLayout Example 2
33.Layout ExampleLayout Example
34.RowLayout ExampleRowLayout Example
35.SWT FillLayout
36.SWY Radial Layout
37.Use of Radial Layout
38.GridLayout with All Options
39.Simplest GridLayout
40.SWT GridLayout: GridSpan
41.SWT RowLayout
42.SWT FillLayout Composite
43.SWT GridLayout: align widgets in a vertical columnSWT GridLayout: align widgets in a vertical column
44.SWT GridLayout: align widgets in a horizontal rowSWT GridLayout: align widgets in a horizontal row
45.Exclude a widget from a GridLayoutExclude a widget from a GridLayout
46.Insert widgets into a grid layoutInsert widgets into a grid layout
47.Align widgets in a GridLayoutAlign widgets in a GridLayout
48.FormLayout: create a simple OK and CANCEL dialog using form layoutFormLayout: create a simple OK and CANCEL dialog using form layout
49.FormLayout: center a label and single line text using a form layoutFormLayout: center a label and single line text using a form layout