Creating a GridLayout

GridLayout works somewhat like RowLayout, in that it controls placement of widgets in rows. However, GridLayout provides much more functionality than RowLayout, such as the ability to specify how many widgets wide a row should be, effectively enabling you to create a grid of rows and columns. Along with this extra functionality come even more additional settings that must be managed, making GridLayout much more complex than RowLayout.

The process for creating a GridLayout is slightly different from the process for creating either a RowLayout or a FillLayout. The process begins the same way—with the creation of an instance of the GridLayout class—but an additional setting must be specified: the number of columns in the grid. This setting controls the number of widgets that will make up a single row of the grid.

How do I do that?

The steps used to create a GridLayout are similar to those used to create a RowLayout, with the exception of having to specify the number of columns. Example 9-4 demonstrates how to create a three-column grid.

Example 9-4. Using GridLayout

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

public class GridLayoutExample {
    Display d;
    Shell s;
    GridLayoutExample( )    {
        d = new Display( );
        s = new Shell(d);
        s.setSize(250,250);
        s.setImage(new Image(d, "c:\\icons\\JavaCup.ico"));
        s.setText("A GridLayout Example");
        GridLayout gl = new GridLayout( );
        gl.numColumns=3; ...

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.