How to right-align text in a SWT Table cell?
I have a SWT table which wrapped by the JFace TableViewer, but this problem also applies to org.eclipse.swt.widgets.Table.
When I use a StyledCellLabelProvider, the text is always left aligned, even when I use
colA.getColumn().setAlignment(SWT.RIGHT);
Here is the label provider and setup:
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);
}
});
Any sort of workaround would be great. For e.g, nesting a widget inside the table and right aligning the text in the widget somehow.
Platform: Windows 7
You found a bug in StyledCellLabelProvider
. It will not occur with any other CellLabelProvider
.
StyledCellLabelProvider
uses "owner draw" for drawing the Table
cells. That means, the cell content is not drawn natively by the OS. It is drawn in an SWT.PaintItem
event by the Table "owner".
StyledCellLabelProvider
does not respect the alignment of the TableColumn
. You can see the source here, the method getTextLayoutForInfo(.)
is of interest.
A workaround could be to copy that class, fix the bug by adding
TableColumn col = ((Table)viewer.getControl()).getColumn(cell.getColumnIndex());
layout.setAlignment(col.getAlignment());
in the method getTextLayoutForInfo(.)
(I didn't test this fix, but if it doesn't work, you should get the idea, and be able to make it work)
You should also add a bug report: Eclipse Bugzilla
精彩评论