Controlling Wrapping of Widgets with RowLayout

RowLayout is more complex than FillLayout and, consequently, you must manage some additional settings to achieve the desired effect. One such setting—wrap—controls whether widgets will wrap down to a second row if the container is resized so that the widgets no longer fit on a single row. The default behavior, seen in the preceding example, is to wrap widgets; however, there are times when your interface design won’t permit wrapping and you must then alter the default RowLayout behavior.

How do I do that?

The row-wrapping feature that is the default for RowLayout can easily be switched off if you want a different effect. The following code creates a RowLayout in which wrapping does not take place:

RowLayout rl = new RowLayout( );
rl.wrap = false;
s.setLayout(rl);

To see the effect, change RowLayoutExample to substitute the previous three lines with this line:

s.setLayout(new RowLayout( ));

When you create an instance of the RowLayoutExample class and resize the window, you will notice that the widgets no longer wrap to a second line when the width of the window is not sufficient to display the entire row. Figure 9-6 shows what happens when the window is resized so that the widgets cannot fit across the width of the window.

Nonwrapping RowLayout

Figure 9-6. Nonwrapping RowLayout

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.