SWT表:自动调整所有列的大小

Mad*_*adH 13 java user-interface swt jface

Qt解决方案是对resizeColumnsToContent()的单个调用,在.NET中可以使用TextRenderer.MeasureText(),JTable可以使用AUTO_RESIZE_ALL_COLUMNS.

在SWT中,是否有一种方法可以在填充列后对programmaticaly进行大小调整?

调用computeSize(SWT.DEFAULT, SWT.DEFAULT)返回相同的值,因此忽略列中剩余的字符.
TableColumn有setWidth(),但是如何在考虑字体外观的情况下获取当前内容的大小提示?

Mad*_*adH 20

解决:

private static void resizeColumn(TableColumn tableColumn_)
{
    tableColumn_.pack();

}
private static void resizeTable(Table table_)
{
    for (TableColumn tc : table.getColumns())
        resizeColumn(tc);
}
Run Code Online (Sandbox Code Playgroud)

  • 不是一行功能相当无用吗?为什么不在resizeTable中调用pack? (14认同)

nan*_*imo 4

在许多情况下,表条目在运行时发生更改以反映数据模型中的更改。向数据模型添加条目也需要调整列的大小,但在我的例子中,修改模型后调用 .pack() 并不能完全解决问题。特别是对于装饰,最后一个条目的大小永远不会调整。这似乎是由于异步表查看器更新所致。这个片段解决了我的问题:

public class LabelDecoratorProvider extends DecoratingStyledCellLabelProvider {

    public LabelDecoratorProvider(IStyledLabelProvider labelProvider,  
        ILabelDecorator decorator, IDecorationContext decorationContext) {
        super(labelProvider, decorator, decorationContext);
    }

    @Override
    public void update(ViewerCell cell) {
        super.update(cell);
        if (TableViewer.class.isInstance(getViewer())) {
            TableViewer tableViewer = ((TableViewer)getViewer());
            Table table = tableViewer.getTable();
            for (int i = 0, n = table.getColumnCount(); i < n; i++)
                table.getColumn(i).pack();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)