我有一个由 JFace TableViewer 包装的 SWT 表,但这个问题也适用于 org.eclipse.swt.widgets.Table。
当我使用 StyledCellLabelProvider 时,文本总是左对齐,即使我使用
colA.getColumn().setAlignment(SWT.RIGHT);
这是标签提供者和设置:
TableViewerColumn colA = new TableViewerColumn(measureTable, SWT.NONE);
colA.setLabelProvider(new StyledCellLabelProvider() {
@Override
public void update(ViewerCell cell) {
ModelItem item = (ModelItem) cell.getElement();
cell.setFont(FONT_REGISTRY.get(MY_SPECIAL_FONT));
cell.setText(item.getText());
super.update(cell);
}
});
任何类型的解决方法都会很棒。例如,在表格中嵌套一个小部件并以某种方式右对齐小部件中的文本。
平台:Windows 7
最佳答案
您在 StyledCellLabelProvider
中发现了一个错误。它不会与任何其他 CellLabelProvider
一起出现。
StyledCellLabelProvider
使用“owner draw”来绘制 Table
单元格。这意味着,单元格内容不是由操作系统本地绘制的。它由表“所有者”在 SWT.PaintItem
事件中绘制。
StyledCellLabelProvider
不遵守 TableColumn
的对齐方式。可以看源here ,方法 getTextLayoutForInfo(.)
很有趣。
解决方法可能是复制该类,通过添加
来修复错误TableColumn col = ((Table)viewer.getControl()).getColumn(cell.getColumnIndex());
layout.setAlignment(col.getAlignment());
在方法 getTextLayoutForInfo(.)
中(我没有测试这个修复,但如果它不起作用,你应该明白了,并且能够让它起作用)
您还应该添加错误报告:Eclipse Bugzilla
关于java - 如何右对齐 SWT 表格单元格中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7255160/