Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In a elipse-rcp application I am setting the background color for a row in a jface table but I don't want the selection to change this color. I want to be able to specify the color change for a selected row.

like image 755
ks. Avatar asked Dec 14 '22 04:12

ks.


2 Answers

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

alt text

like image 156
VonC Avatar answered Feb 15 '23 11:02

VonC


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;
    }
});
like image 45
ks. Avatar answered Feb 15 '23 09:02

ks.