Lining Up Widgets Using RowLayout

RowLayout, as the name implies, enables you to place widgets in a row on a container. When a row has been completely filled, a new row, with all widgets resized appropriately, will be started.

Since RowLayout is more complex and acts in a different manner than FillLayout, the best way to understand RowLayout is to build an example—set the layout manager of a Shell to RowLayout, add a few widgets, and observe the results.

How do I do that?

The first step to using RowLayout is the same as when using FillLayout--create a Shell and call setLayout() to pass an instance of RowLayout. This is demonstrated by Example 9-3.

Example 9-3. Using RowLayout with a shell

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class RowLayoutExample {
    Display d;
    Shell s;
    RowLayoutExample( )    {
        d = new Display( );
        s = new Shell(d);
        s.setSize(250,250);
        s.setImage(new Image(d, "c:\\icons\\JavaCup.ico"));
        s.setText("A RowLayout Example");
        s.setLayout(new RowLayout( ));
        final Text t = new Text(s, SWT.SINGLE | SWT.BORDER);
        final Button b = new Button(s, SWT.BORDER);
        final Button b1 = new Button(s, SWT.BORDER);
        b.setText("OK");
        b1.setText("Cancel");
        s.open( );
        while(!s.isDisposed( )){
            if(!d.readAndDispatch( ))
                d.sleep( );
        }
        d.dispose( );
    }
}

Execution of Example 9-3 results in Figure 9-4 being displayed.

Figure 9-4. RowLayout with defaults

What about...

the times when the window isn’t wide enough ...

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.