Using GridLayout (SWT)

You can use the GridLayout with SWT containers to arrange widgets in a grid arrangement.

The SWT GridLayout layout manager arranges widgets in rows and columns. The visual editor provides feedback as you add widgets to the grid. When you add a widget, you can add it to an empty grid cell, or you can force the grid to add a new row or column and place the widget in a new cell. When you add new rows or columns, "empty" cells are created as needed. Because GridLayout requires each cell to contain a widget, a filler label with no text is added the empty cells actually include a filler label with no text.

The visual editor displays a grid border and placement indicators to help you determine where widgets are located within the grid and where a widget will be placed in relation to other widgets. A yellow bar that spans the entire width or height of the grid indicates that an entire row or column will be added. A yellow bar that spans a single row or column indicates that only that row or column will be affected:

SWT GridLayout

Related concepts
Layout managers and containers
Related tasks
Specifying grid display preferences for containers

Adding or moving components in GridLayout

  1. Select the component from the palette that you want to add to your Grid layout, or click and drag the existing component that you want to move within your Grid layout.
  2. Move your mouse pointer over the position where you want to drop or move the component. As you move the cursor, visual feedback indicates the placement of the widget if you release your mouse button. A yellow bar indicates that a new row or column will be created
  3. Click or release your mouse to drop the component in the position that you selected. Depending on the placement, the visual editor will move widgets and create rows or columns as necessary.
Table 1. Example drop placements and results
Cursor position and feedback Result
Cursor placed in an empty cell:
SWT GridLayout
Widget is added to cell:
SWT GridLayout
Cursor placed on horizontal grid border:
SWT GridLayout
Widget is added to cell in new row:
SWT GridLayout
Cursor placed on vertical grid border:
SWT GridLayout
Widget is added to cell in new column:
SWT GridLayout
Cursor placed on vertical grid border, on single row:
SWT GridLayout
Widget (CLabel) is added to cell, other widgets in row are moved:
SWT GridLayout

Setting the alignment of a component within its cell in the grid

  1. Right-click the component and select Customize Layout. The Customize Layout dialog opens.
  2. In the Alignment section of the Component page, select one of the compass buttons to align the component. For example, if you click the NW (North-West) button, the component will be aligned in the upper left corner of the grid cell.
  3. Click or release your mouse to drop the component in the position that you selected. In Grid layout, you can only place an item to the left or right of another item. Depending on the number of columns and whether other components span columns, the visual editor will move widgets and create rows as necessary.
  4. Optional: To force the row or column that holds the component to grab extra space in the container, click the Fill horizontal or Fill vertical button. If multiple rows or columns are grabbing extra space, they divide up the excess space equally.

Spanning a widget across grid cells

To span a widget across multiple grid cells, do one of the following steps:

Specifying the number of columns and rows in the grid

When you add or move widgets, the number of columns and rows can change dynamically. You can also manually specify the number of columns:
  1. In the Design view, select the container that uses Fill layout.
  2. Click the Customize Layout Customize Layout toolbar button toolbar button. The Customize Layout dialog opens.
  3. On the Layout notebook tab, enter a new value in the Number of columns field.
  4. Optional: You can force all the Grid columns to be the same width by selecting the Make columns equal width check box.

(C) Copyright IBM Corporation 1999, 2004. All Rights Reserved.