用swt组合在headheader中 [英] Combo in columnheader with swt

查看:151
本文介绍了用swt组合在headheader中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个包含组合作为其列表标题之一的表。
我已经发现这个问题无法使用Table:
列标题SWT中的控件(组合,广播,文本)

I want a table that has a combo as one of its columnheaders. I already found out that it is impossible with Table from this question: Controls (Combo, Radio, Text) in column header SWT

有没有办法解决这个问题?我试过TableViewer,但也找不到用它做的方法。
有什么方法可以实现吗?

Is there a way around that? I tried TableViewer but didn't find a way to do it with it either. Is there any way this can be achieved?

推荐答案

您可以在<$ c中创建自己的列标题表格上方的$ c> Composite 使用普通控件。

You could create your own column headers in a Composite above the table using normal controls.

然后,您需要调整这些控件的大小以匹配表列大小。一种方法是使用扩展jface TableColumnLayout 的表布局类,并覆盖调用的 setColumnWidths 方法每次列大小更改时,您都可以调整标题控件的宽度。

You will then need to adjust the size of these controls to match the table column sizes. One way to do this is to use a table layout class extending the jface TableColumnLayout and overriding the setColumnWidths method which is called each time the column sizes change, so you can adjust your header control widths.

注意:TableColumnLayout需要位于 Composite 只包含而不是直接在

Note: TableColumnLayout needs to be on a Composite containing just the Table rather than directly on the Table.

布局如下所示:

/**
 * Table column layout extended to manage a separate table header.
 */
public class TableColumnLayoutWithSeparateHeader extends TableColumnLayout
{
  /** Header composite */
  private final Composite _headerComposite;
  /** Right margin adjust */
  private final int _rightMargin;


  /**
   * Constructor.
   *
   * @param headerComposite Header composite
   * @param rightMargin Right margin value
   */
  public TableColumnLayoutWithSeparateHeader(final Composite headerComposite, final int rightMargin)
  {
    super();

    _headerComposite = headerComposite;
    _rightMargin = rightMargin;
  }


  /**
   * {@inheritDoc}
   * @see org.eclipse.jface.layout.TableColumnLayout#setColumnWidths(org.eclipse.swt.widgets.Scrollable, int[])
   */
  @Override
  protected void setColumnWidths(final Scrollable tableTree, final int [] widths)
  {
    super.setColumnWidths(tableTree, widths);

    // Update the header composite

    final Control [] children = _headerComposite.getChildren();

    final int size = Math.min(widths.length, children.length);

    for (int index = 0; index < size; ++index) {
       final GridData data = (GridData)children[index].getLayoutData();

       int width = widths[index];
       if (index == (size - 1))
         width -= _rightMargin;

       data.widthHint = width; 
     }

    _headerComposite.layout();
  }
}

这篇关于用swt组合在headheader中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆