2014-01-16 92 views
1

我有一个表格,并使用自己的自定义LabelProvider显示背景和前景色。选择SWT表单元格前景

this我推导出我不能改变选择背景颜色。因此,我希望能够在选择时更改文本的前景色。然而,我不知道如何去检测是否选择了特定的行,以便我可以提供不同的前景色。

任何帮助将不胜感激,我不太熟练swt。

编辑: 对于任何搜索,这是我做过什么

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; 
      } 
     }); 
    } 
} 

回答

2

下面是示例代码来设置选择背景上SWT表/树项目

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; 
    } 
    }); 
+0

此外,如果你不介意我问。我如何防止它改变悬停颜色? – Quillion