Quillion
Quillion

Reputation: 6476

SWT table cell foreground on selection

I have a Table, and I use my own custom LabelProvider to display background and foreground colors.

From this I have derived that I can not change selection background color. Therefore I would like to be able to change foreground color of the text upon selection. However I have no idea how I would go about detecting if a specific row is selected so that I can pprovide different foreground color.

Any help will be greatly appreciated, I am not too proficient at swt.

EDIT: For anyone searching this is what I did

public static void attachListenerIfWin7(Table table)
{
    if (System.getProperty("os.name").startsWith("Windows") && System.getProperty("os.version").contains("6.1"))
    {
        table.addListener(SWT.EraseItem, new Listener()
        {
            public void handleEvent(Event event)
            {
                event.detail &= ~SWT.HOT;
                if (event.detail != 24 && event.detail != 22 && event.detail != 18)
                    return;
                int clientWidth = ((Composite) event.widget).getClientArea().width;
                GC gc = event.gc;
                Color oldForeground = gc.getForeground();
                Color oldBackground = gc.getBackground();
// hover
                if (event.detail == 24)
                {
                    gc.setBackground(new Color(event.display, new RGB(115, 115, 115)));
                    gc.setForeground(new Color(event.display, new RGB(115, 115, 115)));
                    gc.fillRectangle(0, event.y, clientWidth, event.height);
                }
// selected
                else if (event.detail == 22)
                {
                    gc.setBackground(new Color(event.display, new RGB(37, 37, 37)));
                    gc.setForeground(new Color(event.display, new RGB(105, 105, 105)));
                    gc.fillGradientRectangle(0, event.y, clientWidth, event.height, true);
                }
// selected but out of focus
                else if (event.detail == 18)
                {
                    gc.setBackground(new Color(event.display, new RGB(57, 57, 57)));
                    gc.setForeground(new Color(event.display, new RGB(135, 135, 135)));
                    gc.fillGradientRectangle(0, event.y, clientWidth, event.height, true);
                }
                gc.setForeground(oldForeground);
                gc.setBackground(oldBackground);
                event.detail &= ~SWT.SELECTED;
            }
        });
    }
}

Upvotes: 1

Views: 1932

Answers (1)

sambi reddy
sambi reddy

Reputation: 3085

Here is sample code to set selection background on SWT Table/Tree Items

  table.addListener(SWT.EraseItem, new Listener() {
     public void handleEvent(Event event) {
        event.detail &= ~SWT.HOT;
        if ((event.detail & SWT.SELECTED) == 0) 
          return; 
        int clientWidth = ((Composite)event.widget).getClientArea().width;
        GC gc = event.gc;
       Color oldForeground = gc.getForeground();
        Color oldBackground = gc.getBackground();
        gc.setBackground(event.display.getColor(SWT.COLOR_YELLOW));
        gc.setForeground(event.display.getColor(SWT.COLOR_BLUE));
        gc.fillGradientRectangle(0, event.y, clientWidth, event.height, true);
        gc.setForeground(oldForeground);
        gc.setBackground(oldBackground);
        event.detail &= ~SWT.SELECTED;
     }
  });

Upvotes: 2

Related Questions