2011-02-09 114 views
4

我正在使用标准swt表,您可能知道,默认情况下,当选择项目时为蓝色(windows标准)。当选择无效时,它变为浅灰色。我想重写这两种颜色......我在网上搜索了所有内容,但只能找到一些非常旧的代码,它们不再适用于表格窗口小部件。swt表更改选择项目颜色

下面是我试图覆盖默认的颜色一些示例代码,但它似乎并没有工作(请原谅肮脏的代码,只是试图让一些工作):

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


      } 
     }); 

任何想法都将被大量赞赏:D

回答

1

不知道是否有一个更简单的方法,但你可以通过“所有者绘制”来实现。看到这个SWT Snippet。尽管如此,这有点矫枉过正。

+0

链接是坏的。下面是另一个:http://bingjava.appspot.com/snippet.jsp?id=2211 – 2013-03-01 22:39:39

4

如果您想使用TableViewer来管理您的表格,您可以使用StyledCellLabelProvider来确定单个单元格的颜色/字体/等。 TableViewer将为您处理“所有者绘制”方面的问题。最大的麻烦是设置与TableViewer一起使用的ContentProvider,LabelProvider和输入类。

+0

更多:子类StyledCellLabelProvider,重写update(ViewerCell)方法,使用ViewerCell.getElement()获取行元素,调用cell.setText (),并在单元格中设置颜色。 – 2013-03-01 22:42:41