【问题标题】:SWT table: auto resize all columnsSWT 表:自动调整所有列的大小
【发布时间】:2012-09-09 20:54:43
【问题描述】:

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

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

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

【问题讨论】:

    标签: java user-interface swt jface


    【解决方案1】:

    解决方法:

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

    【讨论】:

    • 一行函数不是很没用吗?为什么不直接在 resizeTable 中调用 pack?
    • 我同意你在上面的例子中,Buttons840。但它们并非没用,如果你想让一个不可读的“单行”更具表现力/可读性 - 以便方法名称更好地表达你所做的事情。
    【解决方案2】:

    在许多情况下,表条目会在运行时更改以反映数据模型中的更改。向数据模型添加条目也需要调整列的大小,但在我的情况下,在修改模型后调用 .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();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-08
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      • 2019-01-20
      相关资源
      最近更新 更多