Free tutorials for Java, Eclipse and Web programming



Eclipse JFace TableViewer - Tutorial

Clemens Muessener, Lars Vogel

Version 1.8

16.12.2009

Revision History
Revision 0.101.07.2007Clemens Muessener
Created Article
Revision 0.212.09.2007Lars Vogel
Revised Article
Revision 0.320.03.2008Hendrik Still
Added Editors, navigation via tab
Revision 0.412.10.2008Marcus Rieck
Added Sort Columns / Show Hide Columns and
Revision 0.521.03.2009Lars Vogel
Re-worked article, added explanation about the general concept of JFace
Revision 0.622.03.2009Lars Vogel
Correct usage of setInput() in View.java
Revision 0.723.03.2009Lars Vogel
Changed project and package name
Revision 0.824.03.2009Lars Vogel
Fixed the sorter coding
Revision 0.929.03.2009Lars Vogel
Added filter
Revision 1.030.03.2009Lars Vogel
Added StyledCellLabelProvider
Revision 1.120.04.2009Lars Vogel
Minor clean-up in PersonEditingSupport
Revision 1.220.05.2009Lars Vogel
Show / Hide using the menu on the table using Eclipse 3.5
Revision 1.327.07.2009Lars Vogel
Fixed StyledLabelProvider
Revision 1.405.08.2009Lars Vogel
Fixed sorter for Persons
Revision 1.502.09.2009Lars Vogel
removed hard-coded image location against Activator.getImageDescriptor()
Revision 1.608.09.2009Lars Vogel
Fixed incorrect import in LabelProvider
Revision 1.710.09.2009Lars Vogel
added TreeNodeContentProvider and LabelProvider explanation
Revision 1.816.12.2009Lars Vogel
LabelProvider is also template for ITableLabelProvider

Eclipse JFace Table

This article explains the usage of Eclipse JFace TableViewer including label and contentprovider, filtering, sorting, and model / view interaction.

This article is based on Eclipse 3.5.


Table of Contents

1. Eclipse JFace viewers
1.1. Overview
1.2. Viewer, content and label provider
1.3. Viewer
1.4. Content provider
1.5. Label provider
2. Overview of the example
3. Create a project
4. Create the model
5. Content and LabelProvider
5.1. Build your ContentProvider
5.2. Build your LabelProvider
5.3. Use the providers
5.4. Run
6. Cell Editors
7. Sort Columns
8. Filter
9. Highlighting elements - StyledCellLabelProvider
10. Show/Hide Columns
11. Commands
11.1. Print the model content
11.2. Add and delete a person to and from the model
11.3. Copy data to the system clipboard
12. Thank you
13. Questions and Discussion
14. Download
15. Links and Literature
15.1. Source Code
15.2. JFace Resources
15.3. Other Resources

1. Eclipse JFace viewers

1.1. Overview

Eclipse JFace viewers allow to display a domain model in a list, tree or table without converting the domain model beforehand.

These viewers provider adaptors which are used to provide the model to the viewer (these are called content provider) and adaptors to define how the model data is displayed in the viewer (these are called label providers).

The Eclipse JFace viewer concept is very flexible as it allows to use the same content provider / label provider in different viewers without changing the underlying data structure.

JFace also allows assign label provider directly to columns instead of one label provider for the whole table.

1.2. Viewer, content and label provider

In working with JFace viewers it is important to understand the concept of viewers, label and content providers. The following provides an overview and lists some typical classes in these areas.

1.3. Viewer

The user interface is represented by the viewer, e.g. in our example the table viewer.

Viewer: Typical viewers are

  • org.eclipse.jface.viewers.ListViewer - Display a simple list

  • org.eclipse.jface.viewers.TreeViewer - Displays a tree

  • org.eclipse.jface.viewers.TableViewer - Displays a table

1.4. Content provider

Responsible for providing objects to the view. It can simply return objects as-is.

Typical content provider are

  • IStructuredContentProvider: Used for lists and tables

  • ITreeContentProvider: Used for trees, has addional methods to determine the children and the parents of elements

1.5. Label provider

Defines how objects are displayed, e.g. which part of the object is used to return the text which is displayed.

Label provider follow usually the template approach; you have an interface which describes and contact and a class which the programmer can use which delivers already reasonable implementations for the interface and the programmer can then choose to override only the relevant methods.

Typical label providers are

Table 1. Label providers

InterfaceTemplate classDescription
ILabelProviderLabelProviderUsed for lists and trees, displays per element a icons and / or a text element.
ITableLabelProviderLabelProviderUsed for tables, displays per element and column a icons and / or a text element