Chapter 12. SWT Tables

Chapter 9 introduced the GridLayout as a method of designing user interfaces in which data was displayed in tables. Although GridLayout can be used to perform this task, the SWT provides a simpler mechanism for displaying data in tabular format—the SWT Table class, part of the org.eclipse.swt.widgets package. A simple table is shown in Figure 12-1.

A simple table

Figure 12-1. A simple table

Creating a Simple Table

The first step in creating a table such as the one shown in Figure 12-1 is to create an instance of the Table class, passing it the parent container and any style attributes. To that object, add instances of the TableColumn class that represent the columns you wish to appear in your table.

How do I do that?

The code to create a Table looks complex, but for the simple table shown in Figure 12-1, it’s straightforward, as shown in Example 12-1.

Example 12-1. Creating a simple table

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

public class TableShellExample {
    
    Display d;
    Shell s;
    TableShellExample( )    {
        d = new Display( );
        s = new Shell(d);
        
        s.setSize(250,200);
        s.setImage(new Image(d, "c:\\icons\\JavaCup.ico"));
        s.setText("A Table Shell Example");
        s.setLayout(new FillLayout( ));
        
        Table t = new Table(s, SWT.BORDER);

        TableColumn tc1 = new TableColumn(t, SWT.CENTER);
        TableColumn tc2 = new ...

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.