Saturday, August 29, 2009

Creating tree table in RCP (Eclipse 3.4 - Ganymede)


In this article, I will explain about how to create tree table functionality. Here, I assume that you are familiar with creating trees and tables.

Creating tree table:

First of all, do not use TableTreeViewer and TableTree to create tree table. They are deprecated as of 3.1. Creating tree table is easy if you know how to work with trees and tables. Tree table is nothing but a tree which has columns. This is basically how you will create tree table.

Let's assume that you have a tree which displays your organization hierarchy (tree of employee names) . But now, you want to convert that into a tree table so that you can also display employee age and salary (in separate columns) in the hierarchy.

this.treeViewer = new TreeViewer(parent);

Tree tree = this.treeViewer.getTree();

final GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);


/*** Tree table specific code starts ***/


TreeColumn treeColumn = new TreeColumn(tree, SWT.LEFT);

treeColumn = new TreeColumn(tree, SWT.LEFT);

treeColumn = new TreeColumn(tree, SWT.LEFT);

TableLayout layout = new TableLayout();
int nColumns = 3;
int weight = 100 / nColumns;
for (int i = 0; i < nColumns; i++) {
layout.addColumnData(new ColumnWeightData(weight));


/*** Tree table specific code ends ***/

this.treeViewer.setContentProvider(new OrgHierachyContentProvider());
this.treeViewer.setLabelProvider(new OrgHierarchyLabelProvider());

If you are familiar with creating tree, you would be familiar with the first few lines (creating tree) and the last few lines (setting content provider and label provider). The middle section (as marked above) deals with the tree table specific code. If you are familiar with creating tables, then you would understand this. Basically, you are saying that header and column lines should be visible in the tree and you are also creating tree columns and setting the layout for the columns.

Tweaking label provider:

Your content provider would be exactly same as you would do for any tree. This is because underlying data is still the same. In this particular case, the content provider provides employee object for every tree node. Only the representation of model data is going to be in tree table form instead of tree. So, once the above code is set up, you need to tweak the label provider so that it provides labels for individual columns in the tree node.

For tree, you would normally implement ILabelProvider and implement getImage and getText (which just had employee name in our case). For tree table, you should implement ITableLabelProvider (just like you would do for tables) and implement getColumnImage and getColumnText. This way for each column we know what employee attribute to return. Below is a snippet of how your label provider would look:

public String getColumnText(final Object element, final int columnIndex) {
if (element instanceof Employee) {
if (columnIndex == 0) {
return (Employee(element)).getName();
} else if (columnIndex == 1) {
return (Employee(element)).getAge();
} else if (columnIndex == 2) {
return (Employee(element)).getSalary();

return "";


Basically you set up tree table as specified above and your data (content provider) provides tree data i.e bean for every node. Then your label provider takes the bean and provides values for different columns in the tree node. That's all you need to create a tree table. So, if you are familiar with trees and tables, creating tree table is very easy.


Trajce Manev said...

Thx man, you help me alot.
I'm a beginer and this was the stuff i needed.

Mohsin Khan said...

Hi Raja

I am using SWT tree component, i need to implement auto sizing each column of the tree. How we can do it.


Prakash D said...

Hi Raja,

Can you please help me out, i am new to eclipse RCP, i am able to create treetable as follows

a1 10 20
a2 10 30
b1 30 40

but i want somthing like this

aa 40 50
a1 10 20
a2 10 30
bb 40 40
b1 30 40

i am not albe to display teh tree level column can you please help me to solve this.

D Prakash