I'm unable to find any examples that use CheckBoxTableCell
in a TableViewerColumn
if someone could provide me with an example implementation I would be very grateful.
I already have a working model that shows some string values but I'm unable to represent a boolean value as a checkbox.
Is it even possible to show anything else except a String in a TableViewerColumn?
Here is an example with a checkbox in the TableViewerColumn
. It uses an image to represent a bolean in the cell.
It creates a custom view which extends ViewPart
with two static fields to hold the image :
Image CHECKED = Activator.getImageDescriptor("icons/checked.gif").createImage();
Image UNCHECKED = Activator.getImageDescriptor("icons/unchecked.gif").createImage();
Then in the createColumns
method just return one of the images depending in the value of the boolean :
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return null;
}
@Override
public Image getImage(Object element) {
if (((Person) element).isMarried()) {
return CHECKED;
} else {
return UNCHECKED;
}
}
});
Please check the link given above for more details.
If you want an editable checkbox, you need to create an EditingSupport object for the column you want to have a checkbox.
Here is an example :
public class CheckBoxColumnEditingSupport extends EditingSupport {
private TableViewer tableViewer;
public CheckBoxColumnEditingSupport(TableViewer viewer) {
super(viewer);
this.tableViewer = viewer;
}
@Override
protected CellEditor getCellEditor(Object o) {
return new CheckboxCellEditor(null, SWT.CHECK);
}
@Override
protected boolean canEdit(Object o) {
return true;
}
@Override
protected Object getValue(Object o) {
ORMData ormData = (ORMData) o;
return ormData.isOrmIndicator();
}
@Override
protected void setValue(Object element, Object value) {
ORMData ormData = (ORMData) element;
ormData.setOrmIndicator((Boolean) value);
tableViewer.refresh();
}
}
And then add that editing support to specific column in your table:
tableViewerColumn.setEditingSupport(new CheckBoxColumnEditingSupport (myTableViewer));
See this turotial on how to use Column Editing Support. Also this detailed post on How to add an editable checkbox at JFace TableViewer