Forcing Widgets to Span Multiple Cells

It is possible to make a widget span across multiple cells of the grid, yielding rows that contain fewer widgets than other rows. Likewise, you can cause a widget to span vertically across cells in a column.

How do I do that?

The GridData verticalSpan and horizontalSpan settings control how many cells a widget spans. Consider the following code:

gd = new GridData(GridData.FILL_HORIZONTAL);
gd.widthHint=30;
t1.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.widthHint=30;
t2.setLayoutData(gd);
gd = new GridData(GridData.FILL_BOTH);
gd.widthHint=30;
gd.verticalSpan = 2;
t3.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.widthHint=30;
t4.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.widthHint=30;
t5.setLayoutData(gd);

Here, one Text widget has been removed from GridLayoutExample and the GridData object that is attached to Text t3 is configured to span two columns vertically:

gd.verticalSpan = 2;

The style of the GridData is also changed to FILL_BOTH so that t3 automatically filsl the entire two-cell span and resizes as the window is resized. The results are shown in Figure 9-10.

Spanning vertical cells

Figure 9-10. Spanning vertical cells

As you can see, the combination of GridData and GridLayout is powerful, but the power comes at the price of having to specify many additional settings. There are easier methods you ...

Get SWT: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.