Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWT table: 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.

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?

like image 401
MadH Avatar asked Jul 06 '10 12:07

MadH


2 Answers

Solved with:

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

}
private static void resizeTable(Table table_)
{
    for (TableColumn tc : table.getColumns())
        resizeColumn(tc);
}
like image 151
MadH Avatar answered Nov 17 '22 04:11

MadH


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();
        }
    }
}
like image 30
nannimo Avatar answered Nov 17 '22 04:11

nannimo