The following code demonstrates how to create a TableViewer using TableColumnLayout, a ContentProvider and a LabelProvider.
For a detailed explanation, check the post Working With JFace TableViewer.
package com.oxbsystems.study.table.app01;
import java.awt.Dimension;
import java.awt.Toolkit;
import org.eclipse.jface.action.CoolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import com.oxbsystems.study.domain.workgroup.Person;
import com.oxbsystems.study.domain.workgroup.Workgroup;
public class TableApp extends ApplicationWindow {
private class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
public String getColumnText(Object element, int columnIndex) {
Person p = (Person) element;
String result = "";
switch(columnIndex){
case 0:
result = p.getFirst();
break;
case 1:
result = p.getLast();
break;
case 2:
result = p.getTitle();
break;
case 3:
result = p.getEmail();
break;
default:
//should not reach here
result = "";
}
return result;
}
}
private static class ContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
Workgroup w = (Workgroup) inputElement;
return w.getMemberSet().toArray();
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
private Table table;
private TableViewer tableViewer;
private Workgroup workgroup;
/**
* Create the application window,
*/
public TableApp() {
super(null);
setShellStyle(SWT.CLOSE | SWT.MIN | SWT.MAX | SWT.RESIZE);
createActions();
addCoolBar(SWT.FLAT);
addMenuBar();
addStatusLine();
}
/**
* Create contents of the application window.
* @param parent
*/
@Override
protected Control createContents(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout(1, false));
//Create the composite
Composite composite = new Composite(container, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
//Add TableColumnLayout
TableColumnLayout layout = new TableColumnLayout();
composite.setLayout(layout);
tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
table = tableViewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tblclmnFirst = tableViewerColumn.getColumn();
layout.setColumnData(tblclmnFirst, new ColumnWeightData(2, ColumnWeightData.MINIMUM_WIDTH, true));
tblclmnFirst.setText("First");
TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tblclmnLast = tableViewerColumn_1.getColumn();
//Specify width using weights
layout.setColumnData(tblclmnLast, new ColumnWeightData(2, ColumnWeightData.MINIMUM_WIDTH, true));
tblclmnLast.setText("Last");
TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tblclmnTitle = tableViewerColumn_2.getColumn();
//Specify width using weights
layout.setColumnData(tblclmnTitle, new ColumnWeightData(4, ColumnWeightData.MINIMUM_WIDTH, true));
tblclmnTitle.setText("Title");
TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tblclmnEmail = tableViewerColumn_3.getColumn();
//Specify width using weights
layout.setColumnData(tblclmnEmail, new ColumnWeightData(6, ColumnWeightData.MINIMUM_WIDTH, true));
tblclmnEmail.setText("Email");
tableViewer.setLabelProvider(new TableLabelProvider());
tableViewer.setContentProvider(new ContentProvider());
initWorkgroup();
return container;
}
private void initWorkgroup() {
Workgroup w = new Workgroup();
Person p = new Person();
p.setFirst("John");
p.setLast("Smith");
p.setTitle("Manager");
p.setEmail("jsmith@somecompany.com");
w.add(p);
this.setWorkgroup(w);
}
private void add(Person person){
workgroup.add(person);
this.tableViewer.add(person);
this.tableViewer.refresh();
}
private void remove(Person person){
workgroup.remove(person);
this.tableViewer.remove(person);
this.tableViewer.refresh();
}
public void setWorkgroup(Workgroup workgroup){
this.workgroup = workgroup;
this.tableViewer.setInput(workgroup);
}
/**
* Create the actions.
*/
private void createActions() {
// Create the actions
}
/**
* Create the menu manager.
* @return the menu manager
*/
@Override
protected MenuManager createMenuManager() {
MenuManager menuManager = new MenuManager("menu");
return menuManager;
}
/**
* Create the coolbar manager.
* @return the coolbar manager
*/
@Override
protected CoolBarManager createCoolBarManager(int style) {
CoolBarManager coolBarManager = new CoolBarManager(style);
return coolBarManager;
}
/**
* Create the status line manager.
* @return the status line manager
*/
@Override
protected StatusLineManager createStatusLineManager() {
StatusLineManager statusLineManager = new StatusLineManager();
return statusLineManager;
}
/**
* Launch the application.
* @param args
*/
public static void main(String args[]) {
try {
TableApp window = new TableApp();
window.setBlockOnOpen(true);
window.open();
Display.getCurrent().dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Configure the shell.
* @param newShell
*/
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("TableApp");
}
/**
* Return the initial size of the window.
*/
@Override
protected Point getInitialSize() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
return new Point(dim.width,dim.height);
}
}

Hi Octavio Berlanga,
Could the table column be re-sized by user? I mean when i create the table as the way you posted, the table shows exactly as the picture showing. But when i re-size the table column in the UI, the side columns will re-size automatically, which will cause the disappear of side column.
I would be very appreciate if you could help me to figure it out.
Thanks.
The last argument of the ColumnWeightData constructor determines whether the user will be able to resize the column:
layout.setColumnData(tblclmnFirst, new ColumnWeightData(2, ColumnWeightData.MINIMUM_WIDTH, true));
As you point out, all other columns will be resized automatically. The example uses only ColumnWeightData, but there are situations in which it makes sense to also use ColumnPixelData for some columns (not all). Perhaps you will get a behavior that is closer to what you expect by using the following code for the first column:
layout.setColumnData(tblclmnFirst, new ColumnPixelData(150, true, true));
This will set the first column to 150 pixels and the column will not resize if other columns get resized. However, if the first column gets resized, the other columns will get resized as well.
Hi Octavio Berlanga,
You made a nice example, which is probably very usefull for me.
I was just wondering, is it somehow possible to see what code is in the classes:
com.oxbsystems.study.domain.workgroup.Person and
com.oxbsystems.study.domain.workgroup.Workgroup ?
Because I still do not really understand how you can return the Workgroup in the ContentProvider.getElements(Object InputElement)
Would be great if you can provide it to me.
Kind regards,
Nienke W.
There is nothing special about the Person and Workgroup classes.
Person is a POJO with getters and setters for first (name), last (name), title, and email.
Workgroup is another POJO that encapsulates a Set containing objects of type Person using instance variable memberSet.
So, to load the table viewer I pass a workgroup as the input object as in: tableviewer.setInput(workgroup).
You don’t see this happening anywhere in the code, but eventually eclipse passes the workgroup to the ContentProvider getElements method that you mention. I know the input element is a Workgroup, so in the getElements method I cast inputElement to Workgroup. I then use the workgroup’s memberSet to derive the array of objects that the method must return:
public Object[] getElements(Object inputElement) {
Workgroup w = (Workgroup) inputElement;
return w.getMemberSet().toArray();
}
I hope this clarifies.
Octavio