Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide/delete column in SWT table

Tags:

java

swt

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.
like image 998
Ivan Sopov Avatar asked Jun 28 '11 20:06

Ivan Sopov


2 Answers

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 );
like image 121
Mario Marinato Avatar answered Sep 20 '22 20:09

Mario Marinato


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.

like image 25
the.duckman Avatar answered Sep 23 '22 20:09

the.duckman