Reputation: 19
I have a similar use case as mentioned here. I want to change the SWT Table Item selection background color from default grey or blue to some other color. I tried using the StyledCellLabelProvider#update method but it was of no use. It simply updates the background color for all table items to the given color. But I need it to be only for the selected item. Below is the code Snippet of my label provider update method
@Override
public void update(ViewerCell cell) {
cell.setText(provider.getDisplay((T) cell.getElement(), cell.getColumnIndex()));
TableItem currentTableItem = ((TableItem) cell.getViewerRow().getItem());
if (currentTableItem.getParent().getSelectionCount() > 0) {
TableItem selectedTableItem = currentTableItem.getParent().getSelection()[0];
if(currentTableItem == selectedTableItem) {
cell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION));
}
}
cell.setForeground(provider.getDisplayColor((T) cell.getElement(), cell.getColumnIndex()));
super.update(cell);
}
Thanks in advance for the help!
Upvotes: 1
Views: 990
Reputation: 111142
The selection colour is normally chosen by the OS, so to change the colour you have to turn off the selected flag in the StyledCellLabelProvider
erase
, measure
and paint
methods. You also have to draw the selection colour yourself in the erase
method.
Something like the following:
@Override
protected void erase(final Event event, final Object element)
{
if ((event.detail & SWT.SELECTED) != 0) {
event.detail &= ~SWT.SELECTED;
Rectangle bounds = event.getBounds();
event.gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION));
event.gc.fillRectangle(bounds);
}
super.erase(event, element);
}
@Override
protected void measure(final Event event, final Object element)
{
event.detail &= ~SWT.SELECTED;
super.measure(event, element);
}
@Override
protected void paint(final Event event, final Object element)
{
event.detail &= ~SWT.SELECTED;
super.paint(event, element);
}
Upvotes: 2