Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize the column of a table to fill all the available space

Tags:

java

eclipse

swt

I have this table in Eclipse SWT, with 5 columns. I resize the window and together with that the whole table but the columns don't get resized to fill all the available space.

Is there a layout method I can use in order to make a column resize itself to fill all the available space? I found some code that makes the columns resize when the client space has been resized but that seems like a small hack to me.

There surely must be a decent elegant way of doing this by using the layout itself.

like image 653
Mircea Avatar asked Feb 09 '12 12:02

Mircea


People also ask

How do you resize a table or columns to fit the size of your content?

To adjust a column, select it, and then select Layout > AutoFit > AutoFit Contents. To adjust a table, select it, and then select Layout > AutoFit > AutoFit Contents.

How do I resize a column in Word?

On the Page Layout or Layout tab, click Columns. At the bottom of the list, choose More Columns. In the Columns dialog box, adjust the settings under Width and spacing to choose your column width and the spacing between columns.

How do you resize a table row or column to exact dimensions?

Resize rows, columns, or cells On the Layout tab, you can specify the custom height and width. To resize specific rows or column, click on a cell and then adjust the row/column. To make multiple rows or columns the same size, select the columns or rows and click Distribute Rows or Distribute Columns.


1 Answers

This might come in handy:

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

The layoutTableColumns method

  /**
   * 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));
  }
like image 122
npeder Avatar answered Nov 03 '22 18:11

npeder