Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » [Databinding] Calculting the sum of a column in a tableviewer
[Databinding] Calculting the sum of a column in a tableviewer [message #667168] Wed, 27 April 2011 08:15 Go to next message
Christian Hager is currently offline Christian HagerFriend
Messages: 53
Registered: July 2009
Location: Germany
Hello everyone,

I'm having a little problem creating and binding the sum of a column in a tableviewer. I have a list of objects I bound to a tableviewer. One column displays a time. So far everything works fine.
Now I want to create a sum of the time column and display it in a separate text-field. Unfortunately I couldn't find out how to do that and was hoping maybe someone here can give me a hint or point me to any sample where something like this is done.

Thanks in advance for any help.

Re: [Databinding] Calculting the sum of a column in a tableviewer [message #759685 is a reply to message #667168] Tue, 29 November 2011 15:37 Go to previous messageGo to next message
membersound is currently offline membersoundFriend
Messages: 5
Registered: November 2011
Junior Member
Have you found any solution? I'm facing exactly the same problem.
Phps someone knows how to deal with it?

icon3.gif  Re: [Databinding] Calculting the sum of a column in a tableviewer [message #760109 is a reply to message #759685] Thu, 01 December 2011 10:57 Go to previous message
Christian Hager is currently offline Christian HagerFriend
Messages: 53
Registered: July 2009
Location: Germany

yes I found a solution. Basically I ended up binding the list of objects backing my tableviewer to the tableviewer. Then I added this list to a computed value which in turn is bound to the text field. So everytime I change the contents of the list my sum field will update displaying the new sum. I hope the following snippet will display the idea.

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Iterator;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.core.databinding.observable.value.ComputedValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ViewerSupport;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
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 org.eclipse.swt.widgets.Text;

 * Binds a TableViewer to a Collection and displays the sum of the shown column
 * in a text field.
public class SnippetTableViewerSum {

	public static void main(String[] args) {
		final Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				ViewModel viewModel = new ViewModel();
				Shell shell = new View(viewModel).createShell();
				while (!shell.isDisposed()) {
					if (!display.readAndDispatch()) {

	public static abstract class AbstractModelObject {
		private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(

		public void addPropertyChangeListener(PropertyChangeListener listener) {

		public void addPropertyChangeListener(String propertyName,
				PropertyChangeListener listener) {

		public void removePropertyChangeListener(PropertyChangeListener listener) {

		public void removePropertyChangeListener(String propertyName,
				PropertyChangeListener listener) {

		protected void firePropertyChange(String propertyName, Object oldValue,
				Object newValue) {
			propertyChangeSupport.firePropertyChange(propertyName, oldValue,

	static class Task extends AbstractModelObject {
		Double timeTaken = null;

		public Task(Double timeTaken) {
			this.timeTaken = timeTaken;

		public Double getTimeTaken() {
			return timeTaken;

		public void setTimeTaken(Double timeTaken) {
			Double oldValue = this.timeTaken;
			this.timeTaken = timeTaken;
			firePropertyChange("timeTaken", oldValue, timeTaken);

	static class ViewModel {
		private WritableList tasks = new WritableList();

			tasks.add(new Task(Math.random()));
			tasks.add(new Task(Math.random()));
			tasks.add(new Task(Math.random()));
			tasks.add(new Task(Math.random()));
			tasks.add(new Task(Math.random()));
			tasks.add(new Task(Math.random()));
			tasks.add(new Task(Math.random()));

		public WritableList getTasks() {
			return tasks;

	static class View {
		private ViewModel viewModel;
		private Table tasks;
		private Button addTask;
		private TableColumn column;
		private SumValue sumValue;

		public View(ViewModel viewModel) {
			this.viewModel = viewModel;

		public Shell createShell() {

			Display display = Display.getDefault();

			DataBindingContext dbc = new DataBindingContext();

			Shell shell = new Shell(display);
			shell.setLayout(new GridLayout(2, false));

			GridDataFactory gdf = GridDataFactory.swtDefaults()
					.align(SWT.FILL, SWT.FILL).grab(true, true);

			tasks = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
			column = new TableColumn(tasks, SWT.NONE);

			TableViewer tasksViewer = new TableViewer(tasks);
			ViewerSupport.bind(tasksViewer, viewModel.getTasks(),
					BeanProperties.value(Task.class, "timeTaken"));


			GridDataFactory gdf2 = GridDataFactory.swtDefaults().align(
			addTask = new Button(shell, SWT.PUSH);
			addTask.setText("Add Task");
			addTask.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					viewModel.getTasks().add(new Task(Math.random()));


			GridDataFactory gdf3 = GridDataFactory.swtDefaults()
					.align(SWT.FILL, SWT.BEGINNING).grab(true, false);
			Text sum = new Text(shell, SWT.BORDER);
			sumValue = new SumValue(viewModel.getTasks());
			dbc.bindValue(SWTObservables.observeText(sum, SWT.None), sumValue,
					new UpdateValueStrategy(false,
							UpdateValueStrategy.POLICY_NEVER), null);

			shell.setSize(300, 300);;
			return shell;

	// The computed value which calculates the sum
	static class SumValue extends ComputedValue {
		private WritableList tasks;

		SumValue(WritableList tasks) {
			this.tasks = tasks;

		protected Object calculate() {
			double sum = 0;
			Iterator<Task> iterator = tasks.iterator();
			while (iterator.hasNext()) {
				Task task = (Task);
				sum += task.getTimeTaken();
			return String.valueOf(sum);
Previous Topic:Global Validator Before Loading UI
Next Topic:Extended ErrorDialog
Goto Forum:

Current Time: Sat May 26 19:49:32 GMT 2018

Powered by FUDForum. Page generated in 0.02229 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top