简体   繁体   English

如何在SWT表单元格中右对齐文本?

[英]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. 我有一个由JFace TableViewer包装的SWT表,但是这个问题也适用于org.eclipse.swt.widgets.Table。

When I use a StyledCellLabelProvider, the text is always left aligned, even when I use 当我使用StyledCellLabelProvider时,即使我使用,文本也始终保持对齐

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 eg, nesting a widget inside the table and right aligning the text in the widget somehow. 例如,在窗口内嵌套窗口小部件并以某种方式右键对齐窗口小部件中的文本。

Platform: Windows 7 平台:Windows 7

You found a bug in StyledCellLabelProvider . 您在StyledCellLabelProvider发现了一个错误。 It will not occur with any other CellLabelProvider . 任何其他CellLabelProvider都不会发生这种情况。

StyledCellLabelProvider uses "owner draw" for drawing the Table cells. StyledCellLabelProvider使用“所有者绘制”来绘制Table单元格。 That means, the cell content is not drawn natively by the OS. 这意味着,操作系统不会本机绘制单元格内容。 It is drawn in an SWT.PaintItem event by the Table "owner". 它由表“owner”在SWT.PaintItem事件中绘制。

StyledCellLabelProvider does not respect the alignment of the TableColumn . StyledCellLabelProvider不尊重TableColumn的对齐方式。 You can see the source here , the method getTextLayoutForInfo(.) is of interest. 你可以在这里看到源码,方法getTextLayoutForInfo(.)是有意义的。

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) 在方法getTextLayoutForInfo(.) (我没有测试此修复,但如果它不起作用,你应该得到这个想法,并能够使它工作)

You should also add a bug report: Eclipse Bugzilla 您还应该添加错误报告: Eclipse Bugzilla

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM