2011-08-31 98 views
3

我有一个由JFace TableViewer包装的SWT表,但是这个问题也适用于org.eclipse.swt.widgets.Table。如何在SWT表格单元格中右对齐文本?

当我使用一个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的

回答

4

您在StyledCellLabelProvider发现了一个错误。它不会与其他CellLabelProvider发生。

StyledCellLabelProvider使用“所有者绘制”绘制Table单元格。这意味着,单元格内容不是由操作系统本地绘制的。它由表“所有者”在SWT.PaintItem事件中绘制。

StyledCellLabelProvider不尊重TableColumn的对齐方式。你可以看到源here,方法getTextLayoutForInfo(.)是感兴趣的。

一种解决方法可能是通过该方法getTextLayoutForInfo(.)加入

TableColumn col = ((Table)viewer.getControl()).getColumn(cell.getColumnIndex()); 
layout.setAlignment(col.getAlignment()); 

来复制类,修正错误(我没有测试此修复程序,但如果它不工作,你应该得到的想法,并且能够使它工作)

您还应该添加一个bug报告:Eclipse Bugzilla

+0

我不知道我为什么即使使用StyledCellLabelProvider,我只是改变了它使用CellLabelProvider和校准工作.. 。 非常感谢。 – geejay