5.1.7. TableViewer classThe TableViewer class wraps the Table widget. A table viewer provides an editable, vertical, multicolumn list of items, which shows a row of cells for each item in the list where each cell represents a different attribute of the item at that row. A table viewer needs to be configured with a label provider, a content provider, and a set of columns. The CheckboxTableViewer enhances this further by adding support for graying out individual items and toggling on and off an associated checkbox with each item. Useful APIs include:
The CheckboxTableViewer adds the following useful APIs:
The example code that follows creates a table viewer with a label provider, content provider, and four columns (see Figure 5-9). Figure 5-9. TableViewer example.
import org.eclipse.jface.viewers.*; import org.eclipse.swt.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class TableViewerExample { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Table Viewer Example"); shell.setBounds(100, 100, 325, 200); shell.setLayout(new FillLayout()); final TableViewer tableViewer = new TableViewer( shell, SWT.SINGLE | SWT.FULL_SELECTION); final Table table = tableViewer.getTable(); table.setHeaderVisible(true); table.setLinesVisible(true); String[] columnNames = new String[] { "First Name", "Last Name", "Age", "Num Children"}; int[] columnWidths = new int[] { 100, 100, 35, 75}; int[] columnAlignments = new int[] { SWT.LEFT, SWT.LEFT, SWT.CENTER, SWT.CENTER}; for (int i = 0; i < columnNames.length; i++) { TableColumn tableColumn = new TableColumn(table, columnAlignments[i]); tableColumn.setText(columnNames[i]); tableColumn.setWidth(columnWidths[i]); } tableViewer.setLabelProvider( new PersonTableLabelProvider()); tableViewer.setContentProvider( new ArrayContentProvider()); tableViewer.setInput(Person.example()); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } After creating the table viewer, the column headers and lines are made visible by calling the setHeaderVisible() and setLinesVisible() methods in the table viewer's underlying table. Four columns are then added to the table with different alignments. The header text and width of each column are set with the setText() and setWidth() methods (see Section 7.8, Auto-sizing Table Columns, on page 316). The label provider is set using the setLabelProvider() method and the content provider is set with the setContentProvider() method. The label provider, PersonTableLabelProvider, returns a text label for each column in the table and does not return an icon. The class looks like this: import org.eclipse.jface.viewers.*; import org.eclipse.swt.graphics.*; public class PersonTableLabelProvider extends LabelProvider implements ITableLabelProvider { public Image getColumnImage( Object element, int) { return null; } public String getColumnText(Object element, int index) { Person person = (Person) element; switch (index) { case 0 : return person.firstName; case 1 : return person.lastName; case 2 : return Integer.toString(person.age); case 3 : return Integer.toString(person.children.length); default : return "unknown " + index; } } } |