Resize the column of a table to fill all the available space
Asked Answered
M

3

6

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.

Mathematical answered 9/2, 2012 at 12:47 Comment(0)
P
5

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));
  }
Pentagrid answered 9/2, 2012 at 13:43 Comment(2)
Here's another snippet: volanakis.de/nuggets/Snippet77withTableColumnLayout.javaInjection
This answer would be better if it started with some kind of summary. For example: "This can be solved by using a TableLayout and ... blah, blah".Tewell
Q
1

This is what, I have tried and it is working fine.

viewer.getControl().addControlListener(new ControlListener() {

        @Override
        public void controlResized(ControlEvent arg0) {
            Rectangle rect = viewer.getTable().getClientArea();
            if(rect.width>0){
                int extraSpace=rect.width/4;
                col1.getColumn().setWidth(extraSpace);
                col2.getColumn().setWidth(extraSpace);
                col3.getColumn().setWidth(extraSpace);
                col4.getColumn().setWidth(extraSpace);
            }
        }

        @Override
        public void controlMoved(ControlEvent arg0) {
            // TODO Auto-generated method stub

        }
    });
Quay answered 19/2, 2018 at 4:6 Comment(1)
Thanks for this solution. Saved a lot of time.Tendon
C
1

I solved it like this:

public static void makeLastColumnAutoExpand(Table table, int minWidth) {
   var resizer = new ControlAdapter() {
      @Override
      public void controlResized(ControlEvent event) {
         var columns = table.getColumns();
         if (columns.length == 0)
            return;

         if (columns.length == 1) {
            columns[0].setWidth(Math.max(minWidth, table.getClientArea().width));
            return;
         }

         var totalWidthOfOtherColumns = 0;
         for (var i = 0; i < columns.length - 1; i++) {
            totalWidthOfOtherColumns += columns[i].getWidth();
         }

         var newWidth = Math.max(minWidth, table.getClientArea().width - totalWidthOfOtherColumns);
         columns[columns.length - 1].setWidth(newWidth);
      }
   };

   table.addControlListener(resizer);
   for (final var col : table.getColumns()) {
      col.addControlListener(resizer);
   }
}

setLastColumnAutoExpand adds a ControlListener to the table and all it's columns. This ensures that all resize operations will trigger auto resizing of the last column.

Canty answered 23/8, 2021 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.