Swt table element color change

I use the standard swt table, which, as you know, by default, when an element is selected, it is colored blue (standard windows). When the selection is inactive, it turns light gray. I would like to override both colors ... I searched all over the network, but could only find very old code that no longer works with table widgets.

Below is an example of code that I tried to overwrite the default color, but it does not seem to work (please excuse the dirty code, just trying to get something to work):

table.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent event) { Color rowSelectionColor = new Color(Display.getCurrent(),new RGB(235, 200, 211)); TableItem item =(TableItem)event.item; item.setBackground(0,rowSelectionColor); item.setBackground(1,rowSelectionColor); item.setBackground(2,rowSelectionColor); } @Override public void widgetDefaultSelected(SelectionEvent event) { Color rowSelectionColor = new Color(Display.getCurrent(),new RGB(235, 200, 211)); TableItem item =(TableItem)event.item; item.setBackground(0,rowSelectionColor); item.setBackground(1,rowSelectionColor); item.setBackground(2,rowSelectionColor); } }); 

Any ideas would be greatly appreciated: D

+4
source share
3 answers

If you want to use TableViewer to manage your table, you can use StyledCellLabelProvider to define colors / fonts / etc for individual cells. TableViewer will take care of the "owner of the drawing" aspects for you. The biggest problem is creating ContentProvider, LabelProvider and input classes that come with TableViewer.

+4
source

This question explains why you cannot change the color of the selected row in the SWT table:

Windows 7 SWT table displays row highlight color

+3
source

I don’t know if there is an easier way, but you can implement this with draw draw. See This SWT Snippet . However, this is overkill.

+1
source

Source: https://habr.com/ru/post/1338974/


All Articles