2013-07-08 52 views
0

有没有什么方法可以设置SWT表列的前景色和/或背景色?还是SWT表头的前景色和背景色? setForeground /的setBackground方法并非适用于org.eclipse.swt.widgets.TableColumn设置swt table列前景色

回答

0

setBackground()setForeground()方法TableItem

如果您希望能够更高效地自定义项目,我建议您改用TableViewer

Here是一个很好的教程与样式的例子。


下面是一个简单的Table用彩色柱一些示例代码:

public static void main(String[] args) 
{ 
    Display display = new Display(); 
    final Shell shell = new Shell(display); 
    shell.setText("StackOverflow"); 
    shell.setLayout(new FillLayout()); 

    Table table = new Table(shell, SWT.NONE); 
    table.setHeaderVisible(true); 

    for(int col = 0; col < 3; col++) 
    { 
     TableColumn column = new TableColumn(table, SWT.NONE); 
     column.setText("Column " + col); 
    } 

    Color color = display.getSystemColor(SWT.COLOR_YELLOW); 

    for(int row = 0; row < 10; row++) 
    { 
     TableItem item = new TableItem(table, SWT.NONE); 

     for(int col = 0; col < 3; col++) 
     { 
      item.setText(col, "Item " + row + " Column " + col); 

      if(col == 1) 
      { 
       item.setBackground(col, color); 
      } 
     } 
    } 

    for(int col = 0; col < 3; col++) 
    { 
     table.getColumn(col).pack(); 
    } 

    shell.pack(); 
    shell.open(); 
    while (!shell.isDisposed()) 
    { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 
    display.dispose(); 
} 

enter image description here

+0

TableItems是表的行。设置背景/前景颜色不会设置表格**标题**前景/背景颜色 –

+0

@ChandrayyaGK您要求输入列**或**标题的背景。使用'TableItem'方法,可以为整个列着色(不必为整行着色),除了作为OS组件的头部(所以我怀疑它可以着色)。 – Baz

+0

@ChandrayyaGK [Here](https://bugs.eclipse.org/bugs/show_bug.cgi?id=63038)是关于标题颜色的错误报告。 – Baz

1

号,不可能设置背景/前景上TableColumn的(取决于本地支持)。您可能必须自定义绘制标题。 使默认页眉不可见,并在单独的画布上绘制自己的标题,并且需要保持其与TableColumn同步并滚动Table

org.eclipse.swt.widgets.Table.setHeaderVisible(boolean)