Search code examples
swteclipse-rcpjfaceeclipse-rap

TableViewer not refreshing in RAP


I tried to run my Eclipse RCP code to run in Eclipse RAP environment. In my Eclipse RCP code, there is functionality to add the rows in to table. But adding the code does not work in Eclipse RAP. I am using TableViewer. Following is my code.

public class BasicEntryPoint extends AbstractEntryPoint {

    private static final int COLUMNS = 2;
    private TableViewer viewer;

    private class ViewContentProvider implements IStructuredContentProvider {
        public Object[] getElements(Object inputElement) {
            List<Person> list = (List<Person>) inputElement;
            return list.toArray();
        }

        public void dispose() {
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }
    }

    private class ViewLabelProvider extends LabelProvider implements
            ITableLabelProvider {
        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }

        public String getColumnText(Object element, int columnIndex) {
            Person p = (Person) element;

            if (columnIndex == 0) {
                return p.getName();
            }

            return p.getPlace();
        }
    }

    private class Person{
        String name;
        String place;

        public void setName(String name) {
            this.name = name;
        }

        public void setPlace(String place) {
            this.place = place;
        }

        public String getName() {
            return name;
        }

        public String getPlace() {
            return place;
        }
    }

    public List<Person> persons() {

        List<Person> list = new ArrayList<Person>();

        Person person = new Person();
        person.setName("bb");
        person.setPlace("jjj");

        list.add(person);

        person = new Person();
        person.setName("sss");
        person.setPlace("fff");
        list.add(person);

        return list;

    }

    @Override
    protected void createContents(Composite parent) {
        parent.setLayout(new GridLayout(2, false));

        viewer = new TableViewer(parent, SWT.NONE);
        viewer.setContentProvider(new ViewContentProvider());
        viewer.setLabelProvider(new ViewLabelProvider());
        final Table table = viewer.getTable();
        viewer.setColumnProperties(initColumnProperties(table));
        viewer.setInput(persons());
        viewer.getTable().setHeaderVisible(true);

        Button checkbox = new Button(parent, SWT.CHECK);
        checkbox.setText("Hello");
        Button button = new Button(parent, SWT.PUSH);
        button.setText("World");
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                System.out.println("Button clicked");
                Person p = new Person();
                p.setName("Dee");
                p.setPlace("TCR");
                persons().add(p);
                String prop[] ={"name","place"};
                viewer.update(p, prop);
                //viewer.refresh();
            }
        });
    }

    private String[] initColumnProperties(Table table) {
        String[] result = new String[COLUMNS];
        for (int i = 0; i < COLUMNS; i++) {
            TableColumn tableColumn = new TableColumn(table, SWT.NONE);
            result[i] = "Column" + i;
            tableColumn.setText(result[i]);
            if (i == 2) {
                tableColumn.setWidth(190);
            } else {
                tableColumn.setWidth(70);
            }
        }
        return result;
    }

}

Solution

  • You should use:

    viewer.add(p);
    

    rather than update to add a new item to a table (both for SWT and RAP).

    You must also update your model to contain the new item.