Support free tutorials:

vogella training Training Books

JFace Data Binding and EMF - Tutorial

Lars Vogel

Version 1.3


Revision History
Revision 0.1 25.09.2010 Lars
Revision 0.2 - 1.3 05.10.2010 - 08.09.2012 Lars
bugfixes and enhancements

JFace Data Binding and Eclipse EMF

This article explains the usage JFace Data Binding for Eclipse EMF, This article is based on Eclipse 4.2 (Juno).

Table of Contents

1. Prerequisistes
2. EMF and Data Binding for SWT controls
3. Support this website
3.1. Thank you
3.2. Questions and Discussion
4. Links and Literature
4.1. Source Code
4.2. Eclipse DataBinding Resources
4.3. vogella Resources

1. Prerequisistes

This article covers JFace Data Binding together with the Eclipse Modeling Framework (EMF) .

This tutorial assumes that you are familiar with the base concepts of EMF and JFace Data binding and will focus on the additional information how to use both frameworks together.

For an introduction to JFace Data Binding please see JFace Data Binding Tutorial

. For an introduction to Eclipse EMF please see Eclipse EMF Tutorial.

2. EMF and Data Binding for SWT controls

Create an RCP project called de.vogella.databinding.emf.swt based on the RCP application with a view . Add the org.eclipse.emf.ecore and the JFace data binding plug-ins as a plug-in dependency.

Create an EMF model called person.ecore. Create a class called Phone with the number property. Create also a class called Person which should have the following properties:

  • firstName - String

  • lastName - String

  • gender - String

  • isMarried - boolean

  • phone - Phone

The result should look similar to the following.

Change the View class according to the following listing. The code demonstrates how you can use Data Binding to access model properties. It also demonstrates how to access nestled properties to the phone class with is part of person.

package de.vogella.databinding.emf.swt;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.emf.databinding.EMFProperties;
import org.eclipse.emf.databinding.FeaturePath;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import de.vogella.databinding.emf.swt.model.ModelFactory;
import de.vogella.databinding.emf.swt.model.ModelPackage;
import de.vogella.databinding.emf.swt.model.Person;
import de.vogella.databinding.emf.swt.model.Phone;

public class View extends ViewPart {

  public static final String ID = "de.vogella.databinding.emf.swt.view";
  private Text firstName;

  public void createPartControl(Composite parent) {
    final Person person = createPerson();

    Layout layout = new GridLayout(2, false);

    firstName = new Text(parent, SWT.NONE);
    GridData gridData = new GridData();
    gridData.horizontalAlignment = GridData.FILL;
    gridData.horizontalSpan = 2;
    Text phoneNumber = new Text(parent, SWT.NONE);
    gridData = new GridData();
    gridData.horizontalAlignment = GridData.FILL;
    gridData.horizontalSpan = 2;

    Button button1 = new Button(parent, SWT.PUSH);
    button1.setText("Write model");
    button1.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {

    Button button2 = new Button(parent, SWT.PUSH);
    button2.setText("Change model");
    button2.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        String reversedNumber = new StringBuffer(person.getPhone()

    DataBindingContext bindingContext = new DataBindingContext();

    FeaturePath feature = FeaturePath.fromList(ModelPackage.Literals.PERSON__PHONE,

  private Person createPerson() {
    // Initialize the model
    // Retrieve the default factory singleton
    ModelFactory factory = ModelFactory.eINSTANCE;

    final Person person = factory.createPerson();
    Phone phone = factory.createPhone();
    return person;

  public void setFocus() {

3. Support this website

This tutorial is Open Content under the CC BY-NC-SA 3.0 DE license. Source code in this tutorial is distributed under the Eclipse Public License. See the vogella License page for details on the terms of reuse.

Writing and updating these tutorials is a lot of work. If this free community service was helpful, you can support the cause by giving a tip as well as reporting typos and factual errors.

3.1. Thank you

Please consider a contribution if this article helped you.

Flattr this

3.2. Questions and Discussion

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.

4. Links and Literature

4.1. Source Code

Source Code of Examples

4.3. vogella Resources

vogella Training Android and Eclipse Training from the vogella team

Android Tutorial Introduction to Android Programming

GWT Tutorial Program in Java, compile to JavaScript and HTML

Eclipse RCP Tutorial Create native applications in Java

JUnit Tutorial Test your application

Git Tutorial Put all your files in a distributed version control system