swt table change selection item color

人走茶凉 提交于 2019-12-10 16:19:14

问题


I'm using a standard swt table which, as you may know, by default when an item is selected is colored blue (windows standard). When the selection is inactive, it turns light gray. I would like to override both colors... I've searched all over the web but could only find some very old code which no longer seems to work with the table widget.

Below is some sample code I was trying to overwrite the default color but it doesn't seem to be working (please excuse the dirty code, was 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 greaaatly massively appreciated :D


回答1:


If you want to go with a TableViewer to manage your table, you can use a StyledCellLabelProvider to determine the colors/fonts/etc for individual cells. The TableViewer will take care of the "owner draw" aspects for you. The biggest hassle is setting up the ContentProvider, LabelProvider, and input classes that go with the TableViewer.




回答2:


This question explains why you can't change the colour of a selected row in an SWT Table:

Windows 7 SWT Table selected row highlight color




回答3:


Don't know if there is a simpler way, but you can implement this with "owner draw". See this SWT Snippet. That's sort of an overkill, though.



来源:https://stackoverflow.com/questions/4941219/swt-table-change-selection-item-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!