Label and Content Providers for JFace TreeViewer

Label and Content Providers for JFace TreeViewer

The JFace library offers some widgets, called viewers, that can represent a data model. What are they for? Let’s suppose you want to show a table, a tree or a combo in order to visualize a list of objects or a hierarchical structure. You could certainly make use of basic widgets like Table, Tree or Combo, but they are usually not a good choice.
Imagine you while filling a table, row by row, starting from a list of Orders and your focus is doing some action with the Order that the user selected.
With a Table widget you have to find a trick like counting on a key value on a certain column, or keep an Order list with the same sequence showed on the table and extract by index on a positional basis from the index of selected row.
Further, just populating the table would be quite a heavy task.

Thanks to JFace viewers, it is possible to set up some sort of “binding” between the widget and the model itself. This allows, for instance, retrieving directly the selected Order object (instead of the index of selected row). This binding is performed by using a couple of providers :

– Label provider and
– Content provider

Here are some notes about the usage of these providers with JFace Viewers. In particular we’ll focus on TreeViewer.
The Model is made of Company entity, which owns a collection of Departments, which in turn owns a collection of Employees.
The goal is to obtain this

The Viewer will receive a list of Company objects. The code that set up the input and that set the providers is the following:

Let’s examine now these providers role:

  • LabelProvider: it is invoked to determine what to show (text and image) for each node that come into view. The method getText() receives the examined object and must return the text to show. The optional method getImage() can be used to supply an image. Here is a possible implementation for our use-case:

  • ContentProvider: it handles the content of the widget. In particular it provides the implementation of the methods that are called when:
    1. when the tree viewer is first shown (with first-level nodes not expanded) (getElements)
    2. when it is required to decide if expansion symbol has to be shown (hasChildren)
    3. when the user choose to expand a node and its children has to be shown (getChildren)

Please note that hasChildren, but also especially getChildren, gets called with a lazy mechanism, that is only when that’s required. As a result, the Employee list of a given Department gets loaded just when the user choose to expand that Department node, not before, with a clear good impact on performances.

The tree shows then all three object’s level (Company, Deparment, Employee)


Note: should the viewer be fed with a single Company object instead of a list, the getElements() method has to be changed like this:

In this case the tree will not show the Company object; instead it will show its children Department (at first level) and the Employee objects (at second level).



2012-06-08T11:51:48+00:00By |


  1. Ram Friday October 12th, 2012 at 05:40 AM - Reply

    Please I Implemented TreeViewer in my application but 3 views are not visible. Juno ide,eclipse rcp.

  2. vivek Monday November 25th, 2013 at 03:58 AM - Reply

    Nice article. I have also written one article on Jface Treee Viewer –

Leave A Comment