9.14. Creating Tables

Problem

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

Solution

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

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 ...

Get Eclipse Cookbook 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.