【发布时间】:2025-12-08 10:15:01
【问题描述】:
有没有办法设置 SWT 表格列的前景和/或背景颜色?或 SWT 表格标题的前景和背景颜色? org.eclipse.swt.widgets.TableColumn
上没有 setForeground/setBackground 方法【问题讨论】:
有没有办法设置 SWT 表格列的前景和/或背景颜色?或 SWT 表格标题的前景和背景颜色? org.eclipse.swt.widgets.TableColumn
上没有 setForeground/setBackground 方法【问题讨论】:
没有。无法在 TableColumn 上设置背景/前景(取决于本机支持)。您可能必须自己自定义绘制标题。
使默认标题不可见并在单独的画布中绘制您自己的标题,您需要使其与TableColumn 和Table 的滚动保持同步。
org.eclipse.swt.widgets.Table.setHeaderVisible(boolean)
【讨论】:
TableItem 中有 setBackground() 和 setForeground() 方法。
如果您希望能够更有效地自定义项目,我建议您改用TableViewer。
Here 是一个带有样式示例的优秀教程。
以下是带有彩色列的简单Table 的一些示例代码:
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("*");
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();
}