代码之家  ›  专栏  ›  技术社区  ›  MadH

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

  •  13
  • MadH  · 技术社区  · 14 年前

    qt解决方案是对 ResizeColumnsToContent()大小 ,在.NET中可以使用 text呈现器.measureText()) ,jtable可以使用 自动调整所有列的大小 .

    在SWT中,有没有一种方法可以在填充列后以编程方式调整列的大小?

    打电话 computeSize(SWT.DEFAULT, SWT.DEFAULT) 返回相同的值,从而忽略列中剩余的字符。
    台柱有 setWidth() ,但如何获取当前内容的大小提示,同时考虑字体等?

    2 回复  |  直到 12 年前
        1
  •  4
  •   nannimo    12 年前

    在许多情况下,表条目在运行时更改以反映数据模型中的更改。向数据模型添加条目也需要调整列的大小,但在我的例子中,修改模型后调用.pack()并不能完全解决这个问题。在带有装饰的particolar中,最后一个条目永远不会调整大小。此接缝是由于异步表查看器更新造成的。这一招解决了我的问题:

    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();
            }
        }
    }
    
        2
  •  20
  •   MadH    14 年前

    解决:

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