I have created a tableViewer with two columns and I want to make one of them a checkbox. To do that I have created a CheckBoxCellEditor, but I don´t know why it isn´t working.
The column called tableName displays it´s values OK.
The column specification is the following.
String[] COLUMN_HEADINGS = {"Select", "Table name"};
TableColumnLayout tableLayout = new TableColumnLayout();
parent.setLayout(tableLayout);
//Set what would display each column
TableViewerColumn selectCheckBox = createTableViewerColumn(COLUMN_HEADINGS[0], 0);
selectCheckBox.getColumn().setResizable(false);
selectCheckBox.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element) {
return null;
}
});
//Set what would display each column
TableViewerColumn tableName = createTableViewerColumn(COLUMN_HEADINGS[1], 1);
tableName.getColumn().setResizable(false);
tableName.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element) {
if(element instanceof TableMetaData && element != null)
return ((TableMetaData)element).getName();
return super.getText(element);
}
});
//Set the dimensions of each column
tableLayout.setColumnData(selectCheckBox.getColumn(), new ColumnWeightData(10));
tableLayout.setColumnData(tableName.getColumn(), new ColumnWeightData(90));
//Set column type (checkbox)
selectCheckBox.setEditingSupport(new ResourcesConfigCheckEditingSupport(this));
And the EditingSupport is the following:
public class ResourcesConfigCheckEditingSupport extends EditingSupport{
private CheckboxCellEditor cellEditor;
public ResourcesConfigCheckEditingSupport(ColumnViewer viewer) {
super(viewer);
// TODO Auto-generated constructor stub
cellEditor = new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
}
@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 false;
}
@Override
protected Object getValue(Object element) {
// TODO Auto-generated method stub
return ((TableMetaData) element).getIsSelected();
}
@Override
protected void setValue(Object element, Object value) {
// TODO Auto-generated method stub
((TableMetaData) element).setIsSelected(Boolean.valueOf((boolean) value));
getViewer().update(element, null);
}
}
The TableMetaData object decides if the Checkbox is going to be selected or not. How can I fix my code in order to make it work?
Thank you.
Vogella has a complete example which you will find here: http://www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html
In section 4 a CheckboxCellEditor is used. It might help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With