Java TableViewer - 在选择时更改单元格图像
Java TableViewer - change cell image on selection
我遵循了 Vogella 关于 Table Viewer 的教程并使用了那里介绍的 StyledCellLabelProvider。它在第三列创建一个 CHECKED 或 UNCHECKED 图标
col = createTableViewerColumn(titles[3], bounds[3], 3);
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;
}
}
});
col.setEditingSupport(new MarriedEditingSupport(viewer));
创建 table 时会相应地绘制列图像,但是如何在用户单击单元格时更改图标? (我想模拟一个复选框)
您可以使用基于 CheckboxCellEditor
的 EditingSupport
class:
class MarriedEditingSupport extends EditingSupport
{
private final CheckboxCellEditor _editor;
MarriedEditingSupport(TableViewer viewer)
{
super(viewer);
_editor = new CheckboxCellEditor(viewer.getTable());
}
@Override
protected CellEditor getCellEditor(Object element)
{
return _editor;
}
@Override
protected boolean canEdit(Object element)
{
return true;
}
@Override
protected Object getValue(Object element)
{
return Boolean.valueOf(((Person)element).isMarried());
}
@Override
protected void setValue(Object element, Object value)
{
((Person)element).setMarried(((Boolean)value).booleanValue());
getViewer().update(element, null);
}
}
我遵循了 Vogella 关于 Table Viewer 的教程并使用了那里介绍的 StyledCellLabelProvider。它在第三列创建一个 CHECKED 或 UNCHECKED 图标
col = createTableViewerColumn(titles[3], bounds[3], 3);
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;
}
}
});
col.setEditingSupport(new MarriedEditingSupport(viewer));
创建 table 时会相应地绘制列图像,但是如何在用户单击单元格时更改图标? (我想模拟一个复选框)
您可以使用基于 CheckboxCellEditor
的 EditingSupport
class:
class MarriedEditingSupport extends EditingSupport
{
private final CheckboxCellEditor _editor;
MarriedEditingSupport(TableViewer viewer)
{
super(viewer);
_editor = new CheckboxCellEditor(viewer.getTable());
}
@Override
protected CellEditor getCellEditor(Object element)
{
return _editor;
}
@Override
protected boolean canEdit(Object element)
{
return true;
}
@Override
protected Object getValue(Object element)
{
return Boolean.valueOf(((Person)element).isMarried());
}
@Override
protected void setValue(Object element, Object value)
{
((Person)element).setMarried(((Boolean)value).booleanValue());
getViewer().update(element, null);
}
}