Previous Page
Next Page

5.1.7. TableViewer class

The 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:

add(Object) Adds the given element to this table viewer. This method should be called (by the content provider) when a single element has been added to the model to cause the viewer to accurately reflect the model. This method only affects the viewer, not the model.

add(Object[]) Adds the given elements to this table viewer. This method should be called (by the content provider) when elements have been added to the model to cause the viewer to accurately reflect the model. This method only affects the viewer, not the model.

cancelEditing() Cancels a currently active cell editor.

editElement(Object, int) Starts editing the given element.

getElementAt(int) Returns the element with the given index from this table viewer.

getTable() Returns this table viewer's table control.

insert(Object, int) Inserts the given element into this table viewer at the given position.

isCellEditorActive() Returns whether there is an active cell editor.

remove(Object) Removes the given element from this table viewer. This method should be called (by the content provider) when a single element has been removed from the model to cause the viewer to accurately reflect the model. This method only affects the viewer, not the model.

remove(Object[]) Removes the given elements from this table viewer. This method should be called (by the content provider) when elements have been removed from the model in order to cause the viewer to accurately reflect the model. This method only affects the viewer, not the model.

reveal(Object) Ensures that the given element is visible, scrolling the viewer if necessary.

setCellEditors(CellEditor[]) Sets the cell editors of this table viewer.

setCellModifier(ICellModifier) Sets the cell modifier of this table viewer.

setColumnProperties(String[]) Sets the column properties of this table viewer.

setLabelProvider(IBaseLabelProvider) The table viewer implementation of this Viewer framework method ensures that the given label provider is an instance of either ITableLabelProvider or ILabelProvider.

The CheckboxTableViewer adds the following useful APIs:

addCheckStateListener(ICheckStateListener) Adds a listener for changes to the checked state of elements in this viewer.

getChecked(Object) Returns the checked state of the given element.

getCheckedElements() Returns a list of elements corresponding to checked table items in this viewer.

getGrayed(Object) Returns the grayed state of the given element.

getGrayedElements() Returns a list of elements corresponding to grayed nodes in this viewer.

setAllChecked(boolean) Sets to the given value the checked state for all elements in this viewer.

setAllGrayed(boolean) Sets to the given value the grayed state for all elements in this viewer.

setChecked(Object, boolean) Sets the checked state for the given element in this viewer.

setCheckedElements(Object[]) Sets which nodes are checked in this viewer.

setGrayed(Object, boolean) Sets the grayed state for the given element in this viewer.

setGrayedElements(Object[]) Sets which nodes are grayed in this viewer.

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;
      }
   }
}


Previous Page
Next Page