DekGenius.com
Previous Section  < Day Day Up >  Next Section

Recipe 9.14 Creating Tables

9.14.1 Problem

You have a lot of data you need to present visually, and you want to arrange that data in columns.

9.14.2 Solution

Use an SWT table based on the Table class. SWT tables can display columns of text, images, checkboxes, and more.

9.14.3 Discussion

Here's a selection of the Table class's methods:


void addSelectionListener(SelectionListener listener)

Adds the listener to the collection of listeners that are notified when the table's selection changes


void deselect(int index)

Deselects the item at the given zero-relative index in the table


TableItem[] getSelection( )

Returns an array of TableItem objects that are selected in the table


int getSelectionIndex( )

Returns the zero-relative index of the item which is currently selected in the table (-1 if no item is selected)


int[] getSelectionIndices( )

Returns the zero-relative indices of the items that are currently selected in the table


boolean isSelected(int index)

Returns true if the item is selected, false otherwise


void select(int index)

Selects the item at the given zero-relative index in the table

As an example (TableApp at this book's site), we'll create a simple table displaying text items that catches selection events. We'll create a new table and stock it with items using the TableItem class, then report which item has been selected in a text widget. Here are some popular TableItem class methods:


boolean getChecked( )

Returns true if the table item is checked, false otherwise


boolean getGrayed( )

Returns true if the table item is grayed, false otherwise


void setChecked(boolean checked)

Sets the checked state of the checkbox for this table item


void setGrayed(boolean grayed)

Sets the grayed state of the checkbox for this table item


void setImage(Image image)

Sets the table item's image


void setText(String string)

Sets the table item's text

Here's how to create the table in this example:

Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);

And here's how you can stock it with TableItem objects:

for (int loopIndex=0; loopIndex < 24; loopIndex++) {
    TableItem item = new TableItem (table, SWT.NULL);
    item.setText("Item " + loopIndex);
}

All that's left is to handle item selection events, which you can do as shown in Example 9-4; you can recover the item selected with the item member of the event object passed to the handleEvent method.

Example 9-4. SWT tables
package org.cookbook.ch09;

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;

public class TableClass
{
    public static void main(String[] args)
    {
        Display display = new Display( );
        Shell shell = new Shell(display);
        shell.setSize(260, 300);
        shell.setText("Table Example");

        final Text text = new Text(shell, SWT.BORDER);
        text.setBounds(25, 240, 200, 25);
        
        Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);

        for (int loopIndex=0; loopIndex < 24; loopIndex++) {
            TableItem item = new TableItem (table, SWT.NULL);
            item.setText("Item " + loopIndex);
        }
        
        table.setBounds(25, 25, 200, 200);
        
        table.addListener(SWT.Selection, new Listener( )
        {
            public void handleEvent(Event event)
            {
                text.setText("You selected " + event.item);
            }
        });

        shell.open( );
        while (!shell.isDisposed( ))
        {
            if (!display.readAndDispatch( ))
                display.sleep( );
        }
        display.dispose( );
    }
}

The results appear in Figure 9-9. When you select an item in the table, the application indicates which item was selected.

Figure 9-9. A simple table
figs/ecb_0909.gif


That's fine up to a point, but this rudimentary example just gets us started with tables (in fact, this simple version looks much like a simple list widget). To add columns, check marks, images, and more, see the following recipes.

By default, tables allow only single selections. To allow multiple selections, create the table with the SWT.MULTI style instead of the SWT.SINGLE style.


9.14.3.1 Eclipse 3.0

In Eclipse 3.0, the SWT table widget supports setting the foreground and background colors of individual cells. In addition, the Table widget enables you to set the font for a row or an individual cell.

9.14.4 See Also

Recipe 9.15 on creating table columns; Recipe 9.16 on adding check marks to table items; Recipe 9.17 on enabling and disabling table items; Recipe 9.18 on adding images to table items.

    Previous Section  < Day Day Up >  Next Section