Over a million developers have joined DZone.

Simplified TableViewer for Editing Purposes

DZone 's Guide to

Simplified TableViewer for Editing Purposes

· Java Zone ·
Free Resource

Using the JFace TableViewer for editing purposes can be rather tedious and is certainly not a job for an Eclipse beginner. The problems essentially come up when we start using TableCellModifier. Some of these problems could be removed by providing simple interfaces like TableRowData which allows setting and getting values from model.

* TableRowData acts as a holder of a model object represented
* as a row in a TableViewer.
* TableRowData can be interpreted by TableDataLabelProvider as a row
* in a TableViewer.
public abstract class TableRowData
* Subclasses should return the display value of a specified column.
* If null is returned, then null is again passed back in setColumnValue.
* A null for a combo implies, nothing would get selected.
public abstract String getColumnDisplayValue(String columnName);

* Subclasses should set the displayValue in their underlying model
* object representing the row.
* The displayValue can be transformed into a business specific value
* and then stored inside the model object representing the row.
* If nothing is selected in a combo, then null is passed in displayValue
* If nothing or empty-text is typed in a text-field, null is passed in displayValue
public abstract void setColumnValue(String columnName, String displayValue);

* Subclasses can override this to display an image besides each cell text.
public Image getColumnImage(String columnName) {
return null;

* Subclasses can override this to indicate that a row is not modifiable.
public boolean canModify() {
return true;

 A table viewer itself could be setup as ....

private void createBookTable(Composite parent)
/* create table viewer */
tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);

GridData data = new GridData(GridData.FILL_BOTH);
data.widthHint = 500;
data.heightHint = 200;
data.verticalSpan = 5;

/* add columns */
List<TableColumnDescriptor> columnDescriptors = new ArrayList<TableColumnDescriptor>();

/* title */
TableColumnDescriptor columnDesciptor = new TableColumnDescriptor(TableColumnDescriptor.TEXT, BookRowData.COLUMN_TITLE, 200);

/* author */
columnDesciptor = new TableColumnDescriptor(TableColumnDescriptor.TEXT, BookRowData.COLUMN_AUTHOR, 200);

/* author */
columnDesciptor = new TableColumnDescriptor(TableColumnDescriptor.COMBO, BookRowData.COLUMN_CATEGORY,
new String[]{

/* setup table for editing */
TableViewerUtil.setupTableForEditing(tableViewer, columnDescriptors);

/* set input model to tableviewer */
BookTableData input = new BookTableData(getModel());

The above is an example for editing a list of books. It illustrates the usage of the above discussed framework on top of JFace TableViewer. Features include adding a book, deleting a book, editing a book, moving a book up or down in the displayed list of books.






Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}