How to hide/delete column in SWT table

Ivan Sopov picture Ivan Sopov · Jun 28, 2011 · Viewed 13.2k times · Source

Which approach to use to have ability to hide/delete columns in the table in SWT (in Eclipse plugin in particular)?

  1. A cannot transfer this functionality to rows, since I need insert and hide(or delete) of both rows and columns.
  2. I tried to delete them with TableColumn.dispose(), but according ColumnWeightData in the layout was not deleted and resetting the whole table layout with new TableLayout did not delete information about the columns from the layout.
  3. I tried to create all the needed columns, and hide with setWidth(0) those that should be hidden/deleted. The sample code that I written is here. This approach is not good: 3.1. It does not scale. In my case the maximum quantity of columns may be several thousand with only few really needed by the user. 3.2. Dealing with resizing is really a hell. AFAIU, even after setResizable(false) column may be resized if the parent component is resized. To deal with it I need to write huge listeners for parent component. I didn't try yet.

So should I

  1. Investigate disposing table columns further and use it?
  2. Stack with setWidth(0) for a while, as I have not met scaling issues yet?
  3. Look in the direction of some third-party table components (Nattable...)? - If yes, preferably open-source, as my Eclipse-plugin is open-source.

Answer

Mario Marinato picture Mario Marinato · Jun 29, 2011

We do that on many of our tables here.

First, we make sure the user does not see what we're doing.

table.setRedraw( false );

Then we remove all columns.

while ( table.getColumnCount() > 0 ) {
    table.getColumns()[ 0 ].dispose();
}

And then we add the needed ones.

ArrayList<Column> columns = getShownColumns();

for ( Column column : columns ) {
    TableColumn tableColumn = new TableColumn( table, column.getStyle() );
    tableColumn.setText( column.getTitle() );
    tableColumn.setWidth( column.getWidth() );
}

And finally we let the user see what we did.

table.setRedraw( true );