February 25, 2021
Hot Topics:

Constructing SWT Layouts

  • By Koray Guclu
  • Send Email »
  • More Articles »


FormLayout is a very flexible layout, like GridLayout, but it works in a completely different way. In GridLayout, you have to plan everything ahead and build your user interface. In contrast to GridLayout, FormLayout is independent from the complete layout. The position and size of the components depend on one Control.

The location of the properties listed in Table 6 can be seen in Figure 9.

FormData and FormAttachment

Each widget within a composite having FormLayout can have FormData to set the different layout properties for a widget.

The FormData object has the properties listed in Table 7.

A FormData object can have 0 or 4 FormAttachment objects. The form attachment can have controls (see Figure 15). The FormAttachment object can have the properties listed in Table 8.

Figure 14. FormLayout Example

The above example shows a form layout created by using the code snippet below.

 Source 7.  FormLayout example
  FormLayout layout= new FormLayout();
  shell.setLayout (layout);

  Button button1 = new Button(shell, SWT.PUSH);
  Button button2 = new Button(shell, SWT.PUSH);
  Button button3 = new Button(shell, SWT.PUSH);

  FormData data1 = new FormData();
  data1 .left    = new FormAttachment(0,5);
  data1 .right   = new FormAttachment(25,0);

  FormData data2 = new FormData();
  data2.left     = new FormAttachment(button1,5);
  data2.right    = new FormAttachment(90,-5);

  FormData data3 = new FormData();
  data3.top      = new FormAttachment(button2,5);
  data3.bottom   = new FormAttachment(100,-5);
  data3.right    = new FormAttachment(100,-5);
  data3.left     = new FormAttachment(25,5);

Figure 15 shows the location of the properties and location of the buttons.

Figure 15. FormLayout Example

Page 4 of 5

This article was originally published on April 15, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date