1:Table创建好之后添加如下代码:
Listener paintListener = new Listener() {
public void handleEvent(Event event) {
switch(event.type) {
case SWT.MeasureItem: {
TableItem item = (TableItem)event.item;
String text = getText(item, event.index);
Point size = event.gc.textExtent(text);
event.width = size.x;
event.height = Math.max(event.height, size.y);
break;
}
case SWT.PaintItem: {
TableItem item = (TableItem)event.item;
String text = getText(item, event.index);
Point size = event.gc.textExtent(text);
int offset2 = event.index == 0 ? Math.max(0, (event.height - size.y) / 2) : 0;
event.gc.drawText(text, event.x, event.y + offset2, true);
break;
}
case SWT.EraseItem: {
event.detail &= ~SWT.FOREGROUND;
break;
}
}
}
String getText(TableItem item, int column) {
return item.getText(column);
}
};
table.addListener(SWT.MeasureItem, paintListener);
table.addListener(SWT.PaintItem, paintListener);
table.addListener(SWT.EraseItem, paintListener);
2. 换行要自己加个“\n”到字符串中,如
TableItem item = new TableItem(table, SWT.NONE, table.getItemCount());
item.setText("first line" + "\n" + "second line");
3.如图所示