AndaP
AndaP

Reputation: 1298

TableViewer cell editor not working - SWT

I am trying to implement an editable table viewer in Eclipse SWT. I think I've done everything ok until now, problem is the table is not editable - nothing happens if I click on any row.

I have registered some CellEditors with all my columns:

CellEditor[] editors = new CellEditor[columnNames.length]; 
editors[0] = new TextCellEditor(table);
//do the above for all columns
tableViewer.setCellEditors(editors);

and then I specify a cell modifier for my table:

tableViewer.setCellModifier(new CellModifier(this));

The CellModifier class looks like this:

public class CellModifier implements ICellModifier{

private DBStructureView dbView;

public CellModifier(DBStructureView view){
    super();
    this.dbView = view;
}

@Override
public boolean canModify(Object element, String property) {
    return true;
}

@Override
public Object getValue(Object element, String property) {
    int columnIndex = dbView.getColumnNames().indexOf(property);

    Object result = null;
    AttributeNode node = (AttributeNode) element;

    switch(columnIndex){
    case 0://row id
            result = node.getRow();
    case 1://name
            result = node.getName();
    case 2://value
            result = node.getValue();
    default:
            result = "unknown";
    }

    System.out.println(result);
    return result;
}

@Override
public void modify(Object element, String property, Object value) {
    int columnIndex = dbView.getColumnNames().indexOf(property);

    TableItem item = (TableItem) element;
    AttributeNode node = (AttributeNode)item.getData();
    String valueString;

    switch(columnIndex){
    case 0:
        valueString = ((String) value).trim();
        node.setRow(valueString);
        break;
    case 1: 
        valueString = ((String) value).trim();
        node.setName(valueString);
        break;
    case 2:
        valueString = ((String) value).trim();
        node.setValue(valueString);
        break;
    default:
        break;
    }   

}
}

Having done all this, what can go wrong?

Thanks in advance!

Upvotes: 2

Views: 5508

Answers (2)

AndaP
AndaP

Reputation: 1298

I have found the bug, it seems I forgot to set the properties for the columns. I added:

tableViewer.setColumnProperties(columnNames);

and now it works fine, I can edit any of my cells!

Upvotes: 3

the.duckman
the.duckman

Reputation: 6406

The switch statement in your getValue() misses the break; in each case, it always sets result to "unknown".

Then, if I compare your code with the example in JFace Snippets, it seems you are missing

tableViewer.update(.);

in modify().

But both issues don't explain why "nothing happens", when you click the cell. Since we don't have a running snippet from you, we can only speculate. Did you debug? When you set breakpoints in getValue() and modify(), and click a cell, do you hit the breakpoints?

Update: Since you write, that your code doesn't get hit at all, the issue is most probably in the code you haven't shown us.

Please read the Javadoc of ColumnViewer.setCellEditor():

Users setting up an editable TreeViewer or TableViewer with more than 1 column have to pass the SWT.FULL_SELECTION style bit

Check if your Table has that style bit.

Upvotes: 0

Related Questions