【问题标题】:How to right-align text in a SWT Table cell?如何右对齐 SWT 表格单元格中的文本?
【发布时间】:2011-08-31 09:38:28
【问题描述】:

我有一个由 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

【问题讨论】:

    标签: java eclipse swt jface


    【解决方案1】:

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

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

    StyledCellLabelProvider 不遵守TableColumn 的对齐方式。可以看源码here,方法getTextLayoutForInfo(.)很有意思。

    一种解决方法可能是复制该类,通过添加来修复错误

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

    在方法getTextLayoutForInfo(.)中(我没有测试这个修复,但是如果它不起作用,你应该明白这个想法,并且能够使它起作用)

    您还应该添加错误报告:Eclipse Bugzilla

    【讨论】:

    • 我不知道为什么我什至使用 StyledCellLabelProvider,我只是将其更改为使用 CellLabelProvider 并且对齐工作......非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 2014-09-20
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    相关资源
    最近更新 更多