Creating Column Headings

It is possible to create a GridLayout that mimics a table for presentation of data. When this is the design effect you wish to achieve, you must create column headings in the first row of the grid. Text widgets are then used to fill out the additional rows in the table.

How do I do that?

Example 9-5 demonstrates how GridLayout can be used to create a table for presentation of data in text fields aligned in a grid.

Example 9-5. Creating a table effect 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;
        s.setLayout(gl);
        final Label l1 = new Label(s, SWT.BORDER);
        l1.setText("Column One");
        final Label l2 = new Label(s, SWT.BORDER);
        l2.setText("Column Two");
        final Label l3 = new Label(s, SWT.BORDER);
        l3.setText("Column Three");
        final Text t1 = new Text(s, SWT.SINGLE | SWT.BORDER);
        final Text t2 = new Text(s, SWT.SINGLE | SWT.BORDER);
        final Text t3 = new Text(s, SWT.SINGLE | SWT.BORDER);
        final Text t4 = new Text(s, SWT.SINGLE | SWT.BORDER);
        final Text t5 = new Text(s, SWT.SINGLE | SWT.BORDER);
        final Text t6 = new Text(s, SWT.SINGLE | SWT.BORDER); s.open( ); while(!s.isDisposed( )){ if(!d.readAndDispatch( ...

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.