June 17, 2018
Hot Topics:

Constructing SWT Layouts

  • April 15, 2004
  • By Koray Guclu
  • Send Email »
  • More Articles »


GridLayout is the most useful and flexible layout.The GridLayout lays out the widgets in grids. Configureable properties of the GridLayout are listed in Table 4.

The properties listed in Table 4 affect all the layout behavior. The GridLayout provides a data object as well. If you need to set a different property for a cell, you need to create a GridData object and set this as the control's layout data object.

The makeColumndEqualWidth property in Table 4 makes all the controls the same size, as shown in Figure 8. This property makes the columns the same size. If you want the widgets also to be the same size, you should need to use GridData object's horizontal/vertical alignment properties, as shown in Figure 12.

Figure 8. GridLayout.makeColumnnsEqualWidth=false,true

The numColumns field must be set. This field specifies the number of columns in the GridLayout. If the number of components added is bigger than the number of the components in a composite, GridLayout adds the component to a new row.

For example, five components added to a GridLayout in Figure 9. The GridLayout in Figure 9 only has four columns. For that reason, button A5 will be added to a the second row.

Figure 9 shows the location of the properties on the UI that are listed in Table 4.

Figure 9. GridLayout Example

Initially, each column can have a different width. If you want to force all columns to be the same size, you must set the makeColumnsEqualWidth variable to true, as shown in Figure 8.

 Listing 6.  GridLayout example
  // ...
  GridLayout gridLayout = new GridLayout();
  gridLayout.numColumns = 5;
  for (int i = 1; i <= 7; i++)
    Button button = new Button(shell, SWT.PUSH);
    button.setText("A" + i);
  // ...

The preceding code snippet creates a new grid layout and sets the number of columns to 5. Afterwards, seven components are added to the composite. The figure below shows the position of the widgets.

Figure 10. GridLayout Example using default properties


  • It is the most powerful layout
  • It puts widgets in rows and columns
  • It provides configurable margins
  • It provides configurable spaces
  • It provides a GridData object


Each widget within a composite having GridLayout can have GridData set a different property for each widget. Configureable properties of the GridData object are listed in Table 5.

Table 5. GridData properties
Variable Default Description
false If true, after resize, the widget will grow enough to fit the remaining space.
SWT.DEFAULT (indicates that no minimum width is specified.) Specifies a minimum width/height for the column.
GridData.BEGINNING(possible values are BEGINNING, CENTER, END, FILL) Specifies how controls will be positioned horizontally/vertically within a cell.
horizontalIndent 0 Specifies the number of pixels of indentation that will be placed along the left side of the cell.
1 Specifies the number of column/row cells that the control will take up.

The effect of the first property listed in Table 5 is illustrated below. The grabExcessHorizontalSpace (or grabExcessVerticalSpace) property can be hard to understand at first. For that reason, the area that is affected by this property is highlighted in the picture below. As you can see, if this property is set to true, the width of the grids will be as large as possible. If you want a widget to fill the horizontal space, you need to use this in combination with the alignment property shown in Figure 12.

Figure 11. GridData.grabExcessHorizontalSpace=false,true

The horizontalAlignment (or verticalAlignment) property sets the alignment of the control. You can use this in combination with the grabExcessHorizontalSpace property.

Figure 12. GridData.horizontalAlignment = GridData.BEGINNING, GridData.CENTER, GridData.END, GridData.FILL

The effect of the horizontalSpace is shown below.

Figure 13. GridData.horizontalSpan=1,2

Page 3 of 5

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

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

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date