Filling the Entire Container with Widgets

The first of the layout classes to master is the simplest—FillLayout . As the name implies, FillLayout causes widgets to completely fill the working area of the container within which they are placed. The code developed earlier demonstrated how to create an instance of FillLayout and attach it to a Shell instance:

s.setLayout(new FillLayout( ));

FillLayout causes widgets added to the container to expand so as to fill the entire container. When the container is resized, the widget is resized so as to continue to fill the container. The preceding LayoutExample provides a platform for experimenting with FillLayout; all that remains is to add widgets and observe the effect.

How do I do that?

Example 9-2 is identical to Example 9-1 except that it adds a multiline text field to demonstrate the effect that FillLayout has upon widget placement and size.

Example 9-2. FillLayout with Widgets

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

public class FillLayoutExample {
    Display d;
    Shell s;
    FillLayoutExample( )    {
        d = new Display( );
        s = new Shell(d);
        s.setSize(250,250);
        s.setImage(new Image(d, "c:\\icons\\JavaCup.ico"));
        s.setText("A FillLayout Example");
        s.setLayout(new FillLayout( ));
        final Text t = new Text(s, SWT.MULTI | SWT.BORDER | SWT.WRAP);
        s.open( );
        while(!s.isDisposed( )){
            if(!d.readAndDispatch( ))
                d.sleep( );
        }
        d.dispose( );
    }
}

Executing this code results ...

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.