13

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

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

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?

4

2 に答える 2

20

Solved with:

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

}
private static void resizeTable(Table table_)
{
    for (TableColumn tc : table.getColumns())
        resizeColumn(tc);
}
于 2010-07-06T13:38:28.857 に答える
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();
        }
    }
}
于 2012-09-09T12:34:26.243 に答える