Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 985689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:12:09+00:00 2026-05-16T05:12:09+00:00

I use the org.eclipse.core.databinding framework to bind some Text fields in an SWT application.

  • 0

I use the org.eclipse.core.databinding framework to bind some Text fields in an SWT application. I add an update strategy to validate the data and to set the value on the model only when the user click on the save button:

    UpdateValueStrategy toModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_CONVERT);
    if (validator != null) {
        toModel.setAfterGetValidator(validator);
    }

    UpdateValueStrategy fromModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE);

    binding = bindingContext.bindValue(SWTObservables.observeText(this, SWT.Modify),
                    BeansObservables.observeValue(pVO, propertyName), toModel, fromModel);

This piece of code works really well.

But how can I do the same on a TableViewer?

I want it to work so that when I add something in the IHM, the model stay unchanged until I call getBindingContext().updateModels();

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:12:10+00:00Added an answer on May 16, 2026 at 5:12 am

    You do not need use the JFace Databinding Framework in TableViewer. Manipulation the structured data is simpler then SWT controls, such TableViewer, ListViewer and TreeViewer. You can use those viewer in the same way:

    • create viewer
    • set content provider
    • set label provider (suggested)
    • set filter (optional)
    • set sorter (optional)

    After the viewer created, just invoke viewer.setInput(data) to put all the things to your viewer.

    There are a list of model:

    TableViewer tableViewer = new TableViewer(parent); 
    
    Table table = tableViewer.getTable(); 
    table.setHeaderVisible(true);      
    table.setLinesVisible(true);`
    
    for (int i = 0; i < COLUMN_NAMES.length; i++) {
        TableColumn tableColumn = new TableColumn(table, SWT.LEFT);
        tableColumn.setText(COLUMN_NAMES[i]);
        tableColumn.setWidth(COLUMN_WIDTHS[i]);
    }
    
    tableViewer.setContentProvider(new ModelContentProvider());
    tableViewer.setLabelProvider(new ModelLabelProvider());
    tableViewer.setInput(models);
    

    The magic happens in the content provider:

    class ModelContentProvider implements IStructuredContentProvider {
    
        @SuppressWarnings("unchecked")
        @Override
        public Object[] getElements(Object inputElement) {
            // The inputElement comes from view.setInput()
            if (inputElement instanceof List) {
                List models = (List) inputElement;
                return models.toArray();
            }
            return new Object[0];
        }
    
    /* ... other methods */
    
    }
    

    Each model will become a TableItem and the model in the TableItem(item.getData()).

    However, a table composed by many columns, you need the LabelProvider to help you mapping the property of model to the TableItem:

    class ModelLabelProvider extends LabelProvider implements
            ITableLabelProvider {
    
        @Override
        public Image getColumnImage(Object element, int columnIndex) {
            // no image to show
            return null;
        }
    
        @Override
        public String getColumnText(Object element, int columnIndex) {
            // each element comes from the ContentProvider.getElements(Object)
            if (!(element instanceof Model)) {
                return "";
            }
            Model model = (Model) element;
            switch (columnIndex) {
            case 0:
                return model.getFoo();
            case 1:
                return model.getBar();
            default:
                break;
            }
            return "";
        }
    }
    

    The propagation of models to viewer is easy. If you will propagate viewer to the binded model, using the CellEditor is simple as well.
    To use CellEditor, you need set the column properties, cell editors and cell modifier to TableViewer:

    tableViewer.setColumnProperties(COLUMNS_PROPERTIES);
    tableViewer.setCellEditors(new CellEditor[] {
            new TextCellEditor(table), new TextCellEditor(table) });
    tableViewer.setCellModifier(new ModelCellModifier(tableViewer));
    

    The CellModifier likes this:

    class ModelCellModifier implements ICellModifier {
        TableViewer viewer;
    
        public ModelCellModifier(TableViewer viewer) {
            this.viewer = viewer;
        }
    
        @Override
        public boolean canModify(Object element, String property) {
            // property is defined by viewer.setColumnProperties()
            // allow the FOO column can be modified.
            return "foo_prop".equals(property);
        }
    
        @Override
        public Object getValue(Object element, String property) {
            if ("foo_prop".equals(property)) {
                return ((Model) element).getFoo();
            }
            if ("bar_prop".equals(property)) {
                return ((Model) element).getBar();
            }
            return "";
        }
    
        @Override
        public void modify(Object element, String property, Object value) {
            if ("foo_prop".equals(property)) {
                TableItem item = (TableItem) element;
                ((Model) item.getData()).setFoo("" + value);
    
                // refresh the viewer to show the changes to our user.
                viewer.refresh();
            }
        }
    }
    

    Everything is simple but there are many steps to make all together.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to use the org.osgi.framework.system.packages.extra property to add service interfaces at runtime. These
I want to use the default XML editor (org.eclipse.wst.xml.ui) of Eclipse in an RCP
What is the use of this file in a RAD : org.eclipse.wst.common.component. The contents
I've installed the STS tools in eclipse to use with grails: http://grails.org/STS+Integration I've encountered
I'd like to use org.hibernate.tool.hbm2ddl.SchemaExport from hibernate-core 4.1.4 to generate/export a Sybase ASE 15.5
I have extended org.eclipse.ui.editors.text.TextEditor to implement a custom editor. For this editor, I have
Given the following code in Eclipse: import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.CompilationUnit; public class
As suggested by the Eclipse documentation, I have an org.eclipse.core.resources.IncrementalProjectBuilder that compiles each source
module/.settings/org.eclipse.jdt.core.prefs module/.settings/org.eclipse.wst.common.component module/META-INF/MANIFEST.MF What should be the global ignore pattern for eclipse settings. If
I want to use org.eclipse.jdt.ui.refactoring.RenameSupport class RenameSupport renameSupport = RenameSupport.create(packageFragment, newName, RenameSupport.UPDATE_REFERENCES); Here I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.