SWT表:自动调整所有列的大小 [英] SWT table: auto resize all columns

查看:467
本文介绍了SWT表:自动调整所有列的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Qt solution is a single call to resizeColumnsToContent(), in .NET one can use TextRenderer.MeasureText(), JTable could use AUTO_RESIZE_ALL_COLUMNS.

在SWT中,有没有办法在填充列后对programmaticaly重新调整列大小?

In SWT, is there a way to programmaticaly resize columns after populating them?

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

Calling computeSize(SWT.DEFAULT, SWT.DEFAULT) returns the same value thus disregarding character left overs in columns.
TableColumn has setWidth(), but how do I obtain the size hint for the current content taking into account font face, etc?

推荐答案

在许多情况下,表条目在运行时更改以反映数据模型中的更改。添加数据模型的条目也需要调整列的大小,但在我的情况下,在修改模型之后调用.pack()并不能完全解决问题。特别是装饰品,最后一个条目从未调整大小。这种接缝是由异步表查看器更新引起的。这个剪辑解决了我的问题:

In many cases, the table entries change at run-time to reflect changes in the data model. Adding entry to the data model requires to resize columns as well, but in my case calling .pack() after the modification of the model does not solved completly the problem. In particolar with decorations the last entry is never resized. This seams to be due to async table viewer update. This snipped solved my problem:

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();
        }
    }
}

这篇关于SWT表:自动调整所有列的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆