简体   繁体   中英

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?

Solved with:

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

}
private static void resizeTable(Table table_)
{
    for (TableColumn tc : table.getColumns())
        resizeColumn(tc);
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM