调整表格列的大小以填充所有可用空间

Mir*_*cea 5 java eclipse swt

我在Eclipse SWT中有这个表,有5列.我调整窗口的大小,并与整个表格一起调整大小,但是列不会调整大小以填充所有可用空间.

是否有一种布局方法可以使列调整大小以填充所有可用空间?我找到了一些代码,当客户端空间调整大小时,会调整列的大小,但这对我来说似乎是个小问题.

通过使用布局本身肯定必须有一个体面的优雅方式.

npe*_*der 5

这可能会派上用场:

Composite tableComposite = new Composite(parent, SWT.NONE);
TableViewer xslTable = new TableViewer(tableComposite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
xslTable.getTable().setLinesVisible(true);
xslTable.getTable().setHeaderVisible(true);
TableViewerColumn stylesheetColumn = new TableViewerColumn(xslTable, SWT.NONE);
stylesheetColumn.getColumn().setText(COLUMN_NAMES[0]);
stylesheetColumn.getColumn().setResizable(false);
TableViewerColumn conceptColumn = new TableViewerColumn(xslTable, SWT.NONE);
conceptColumn.getColumn().setText(COLUMN_NAMES[1]);
conceptColumn.getColumn().setResizable(false);
TableColumnLayout tableLayout = new TableColumnLayout();
tableComposite.setLayout(tableLayout);

layoutTableColumns();
Run Code Online (Sandbox Code Playgroud)

layoutTableColumns方法

  /**
   * Resize table columns so the concept column is packed and the stylesheet column takes the rest of the space
   */
  private void layoutTableColumns()
  {
    // Resize the columns to fit the contents
    conceptColumn.getColumn().pack();
    stylesheetColumn.getColumn().pack();
    // Use the packed widths as the minimum widths
    int stylesheetWidth = stylesheetColumn.getColumn().getWidth();
    int conceptWidth = conceptColumn.getColumn().getWidth();
    // Set stylesheet column to fill 100% and concept column to fit 0%, but with their packed widths as minimums
    tableLayout.setColumnData(stylesheetColumn.getColumn(), new ColumnWeightData(100, stylesheetWidth));
    tableLayout.setColumnData(conceptColumn.getColumn(), new ColumnWeightData(0, conceptWidth));
  }
Run Code Online (Sandbox Code Playgroud)

  • 如果以某种摘要开始,这个答案会更好.例如:"*这可以通过使用`TableLayout`和...... blah,blah*来解决. (3认同)