Resize the table column to fill the entire space

I have this table in Eclipse SWT with 5 columns. I am resizing the window and at the same time the whole table, but the columns are not resized to fill all the free space.

Is there a layout method that I can use to resize a column to fill all the available space? I found code that allows you to resize columns when resizing client space, but for me this seems like a little hack.

Of course, there should be a decent elegant way to do this using the layout itself.

+4
source share
2 answers

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

Method 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)); } 
+5
source

Here is what I tried and it works 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 } }); 
+1
source

Source: https://habr.com/ru/post/1395598/


All Articles