Constructing SWT Layouts
RowLayout
RowLayout is very similar to FillLayout. It positions the controls, similar to FillLayout, in rows or columns. In addition to that, RowLayout provides configuration fields to control the position of a control within a composite as seen in Listing 4.
Figure 2 shows a simple use of RowLayout. You can use the preceding code to create the following example. All values are default; nothing is changed. As you can see, it wrapped the row automatically when there is not enough space left in the row.

Figure 2. RowLayout Example with default properties
If you do not want to make any configuration changes, you can use the default values by creating a RowLayout using its default constructor.
The configurable properties of RowLayout are listed in the following table. The variables are member fields in RowLayout. You can access them directly.
Table 2. RowLayout properties | ||
Variable | Default | Description |
justify | false | Spreads the widgets across the available space within a composite. |
marginBottom marginLeft marginRight marginTop spacing |
3 | Number of pixels around widgets. Spacing represents number of pixels between widgets. |
pack | true | Forces all components to be the same size within a composite. |
type | SWT.HORIZONTAL | Positions the widgets in rows or columns. |
wrap | true | Wraps the widgets in row/column if there is not enough space on the row/column |
Figure 6, shown below, shows the fields that do not depend on the size of the composite. Figures 3, 4, and 5 show the properties that depend on the size of the composite.
Figure 3 shows the effect of the wrap property. The wrap is set to false and shell window is resized. As it is shown, there is no wrapping after resize when the wrap is set to false. In contrast to Figure 2 above, the controls on the composite do not wrap.
Figure 3. RowLayout.wrap=false
The pack property forces all components to be the same size within the composite. This property sets the width of the controls to the widest and height of the components to the highest. Figure 4 shows both cases.
Figure 4. RowLayout.pack=true, false
The justify property spreads the widgets across the available space within a composite. Figure 5 shows the effect of the justify property.
Figure 5. RowLayout.justify=false, true
Figure 6 shows some properties listed in Table 2.
Figure 6. RowLayout properties
RowLayout is similar to FillLayout, but has the following key advantages:
- If the number of widgets do not fit in a row, it wraps the widgets.
- It provides configurable margins.
- It provides configurable spaces.
- It provides RowData object.
3.1. RowData
Each widget can have different RowData objects in RowLayout. You can specify different a height and width for each widget in the composite having RowLayout.
Table 3. RowData properties | ||
Variable | Default | Description |
height width |
0 | Height and width of the widget |
RowData only has two properties. There are only the width and height properties that we can use to set the width and height of each widget. If you want to change the size of a widget, you should create a RowLayout for the composite and you should create a RowData object for each widget. You can specify different RowData objects for each widget.
Listing 5. RowData Example |
// ... RowLayout rowLayout = new RowLayout(); shell.setLayout(rowLayout); for (int i = 1; i < 6; i++) { Button button = new Button(shell, SWT.PUSH); button.setText("A" + i); RowData rowData = new RowData(i*15,i*25); button.setLayoutData(rowData); } // ... |
Listing 5 shows a simple RowData example. This code will generate buttons having different heights and widths.
Figure 7. RowData Example
Page 2 of 5