15

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.
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ivan Sopov
  • 2,300
  • 4
  • 21
  • 39

2 Answers2

21

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 );
Mario Marinato
  • 4,561
  • 3
  • 29
  • 49
  • Not sure is it right to comment on already answered and closed question. **But what should I do with layout?** The only working for me thing is TableLayout with strict number of ColumnWeightData or ColumnPIxelData in it, but after removing columns as you wrote - I cannot add any. – Ivan Sopov Jun 29 '11 at 17:31
  • I do not understand your question. Can you express yourself better? – Mario Marinato Jun 29 '11 at 18:57
  • Sorry, I spent yet another evening googling and found that there is TableColumnLayout class. It worked just fine for me. Actually googling about other technologies that I used was a bit easier. – Ivan Sopov Jun 29 '11 at 19:22
3

I'd recreate the table columns each time with the visible columns only. If you use the SWT.VIRTUAL style bit, this will be reasonably fast. Set table.setRedraw(false), remove the data from your Table, dispose all TableColumns, recreate the neccessary ones and set your data again. Then set table.setRedraw(true). This minimizes flickering.

I did all this, it worked fine, the disposal of the TableColumns worked as expected.

Using SWT.VIRTUAL is not for the faint-hearted, though. It implicates a different handling of your Table. You might try without that first, to see if it is fast enough.

Having a table with thousands of columns and only showing a few to the user sounds very strange to me. With the native Table implementations I expect issues with that.

the.duckman
  • 6,376
  • 3
  • 23
  • 21