CodyBugstein
CodyBugstein

Reputation: 23322

Make TableViewer columns automatically take on the width of its longest field

I'm working with a TableViewer and I'm trying to get all the columns to assume the width of its longest item of text.

So far, I've created a TableLayout to add the columns to the table. The problem is that adding a column requires using ColumnWeightData which forces me to specify a size for the column. However, I do not know beforehand exactly what the width of the column should be - it could be anything.

theTableViewer = new TableViewer(comp, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
GridData theTableGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
theTableViewer.getTable().setLayoutData(theTableGridData);
TableLayout layout = new TableLayout();
layout.addColumnData(new ColumnWeightData(100, true));
layout.addColumnData(new ColumnWeightData(100, true));
layout.addColumnData(new ColumnWeightData(100, true));
layout.addColumnData(new ColumnWeightData(100, true));

Does anyone know how to get the TableViewer to automatically size up its columns properly to include everything?

Upvotes: 0

Views: 445

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170745

There is no built-in way to do this. You'll have to listen to events when contents of your table change and calculate the size yourself.

Upvotes: 1

Related Questions