How do I change the background selection color for a jface table

这一生的挚爱 提交于 2019-12-04 09:46:07
VonC

According to this thread, for JFace Viewers (ListViewer, Table, Tree) by means of using EraseItem and MeasureItem events

General principle detailed in the article "Custom Drawing Table and Tree Items"

SWT.EraseItem: allows a client to custom draw a cell's background and/or selection, and to influence whether the cell's foreground should be drawn

ks.
table.addListener(SWT.EraseItem, new Listener() {
    public void handleEvent(Event event) {
        event.detail &= ~SWT.HOT;
        if ((event.detail & SWT.SELECTED) == 0) return; /// item not selected

        Table table =(Table)event.widget;
        TableItem item =(TableItem)event.item;
        int clientWidth = table.getClientArea().width;

        GC gc = event.gc;               
        Color oldForeground = gc.getForeground();
        Color oldBackground = gc.getBackground();

        gc.setBackground(colorBackground);
        gc.setForeground(colorForeground);              
        gc.fillRectangle(0, event.y, clientWidth, event.height);

        gc.setForeground(oldForeground);
        gc.setBackground(oldBackground);
        event.detail &= ~SWT.SELECTED;
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!