Search code examples
eclipse-rcptableviewer

Change the checkbox selection on the basis of ComboBoxCellEditor selection in RCP


I have a table of two columns, which consist of a checkbox in the first column and ComboBoxCellEditor in the second column. When I select something in the ComboBox, the CheckBox of the corresponding row state should change to checked.

tabViewer = new TableViewer(innerTopComp, SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CHECK );

And for ComboBoxCellEditor i have created a class that extends EditingSupport.

public class ComboEditing extends EditingSupport  {

private ComboBoxCellEditor cellEditor;

private String[] comboDataArr;
public ComboEditing( final TableViewer viewer, String[] ComboDataArr) {
    super(viewer);
    this.comboDataArr = ComboDataArr;
    this.cellEditor = new ComboBoxCellEditor(((TableViewer)viewer).getTable(), this.comboDataArr, SWT.DROP_DOWN);  
}

@Override
protected CellEditor getCellEditor(Object element) {
    // TODO Auto-generated method stub
    return cellEditor;
}

@Override
protected boolean canEdit(Object element) {
    // TODO Auto-generated method stub
    return true;
}

@Override
protected Object getValue(Object element) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
protected void setValue(Object element, Object value) {
    // TODO Auto-generated method stub
    if((element instanceof TableData) && (value instanceof Integer)) {
        Integer choice = (Integer)value;
        String option = comboDataArr[choice];
        ((TableData)element).setMatches( option );
        getViewer().update(element, null);

    }
}

}

How to check the checkbox corresponding to the ComboBox in the row, when something is selected in the ComboBox.


Solution

  • You should use CheckboxTableViewer for tables using SWT.CHECK as it provides lots of methods for dealing with the check boxes.

    CheckboxTableViewer tabViewer = CheckboxTableViewer.newCheckList(innerTopComp, SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
    

    CheckboxTableViewer extends TableViewer so your existing code will still be OK.

    You can then use the setChecked method in your EditingSupport setValue method:

    CheckboxTableViewer viewer = (CheckboxTableViewer)getViewer();
    
    viewer.setChecked(element, ... true or false);