Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » How do I remove columns after I've created TableViewer?
How do I remove columns after I've created TableViewer? [message #550338] Thu, 29 July 2010 23:35 Go to next message
Jon Svede is currently offline Jon SvedeFriend
Messages: 83
Registered: July 2009
Member
I have an RCP app using an EMF model running on Galileo. I have a View open in a perspective, when a user opens the XMI file for the model I want the View's TableViewer to be updated. The number of columns can vary, is the only thing. With each model that the user opens, the number of columns may change.

What is the proper way to accomplish this? I have been looking all over and I've tried a lot of things (to the point of confusion).

Thanks in advance,

Jon
Re: How do I remove columns after I've created TableViewer? [message #550377 is a reply to message #550338] Fri, 30 July 2010 07:16 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
You call TableColumn.dispose()

Tom

Am 30.07.10 01:35, schrieb Jon Svede:
> I have an RCP app using an EMF model running on Galileo. I have a View
> open in a perspective, when a user opens the XMI file for the model I
> want the View's TableViewer to be updated. The number of columns can
> vary, is the only thing. With each model that the user opens, the
> number of columns may change.
>
> What is the proper way to accomplish this? I have been looking all over
> and I've tried a lot of things (to the point of confusion).
> Thanks in advance,
>
> Jon
Re: How do I remove columns after I've created TableViewer? [message #550635 is a reply to message #550338] Tue, 03 August 2010 23:45 Go to previous messageGo to next message
Jon Svede is currently offline Jon SvedeFriend
Messages: 83
Registered: July 2009
Member
Thanks! I have tried that, but I have this problem where no matter how I try to remove all the columns, 1 or more remain.

For example, when I do the following:

  TableColumn[] columns = table.getColumns() ;

  for( TableColumn tc : columns ) {
      tc.dispose() ;


I have 1 column left over with no header, just the data. Additionally, from the behavior it appears it starts with the last column and works backwards toward the first (right to left).

When I try this:

  int x = table.getColumnCount() ;
  for( int y=0 ; y<x; y++ )  {
      table.getColumn(y).dispose() ;
  }


It gives me an IndexOutOfBoundsException after the second (of four) column. Then if I get the count again, it says there are still two columns left and if I do another loop it will blow up after the first column - and again if I call table.getColumnCount() it says 1 is left, which I can dispose of but still there is 1 column of data visible. Very strange.

Finally, if I do this:

  int x = table.getColumnCount() ;
  for( int y=x-1; y>-1; y-- )  {
      table.getColumn(y).dispose() ;
  }


It leaves me with 1 column of data (the first one) with no header.

I thought it might have to do with the indexing starting at 1 instead of 0 but it appears that the columns are zero based, so I don't think this is it either. If I change that last for loop to let y become negative it blows up.

Clearly I am doing something wrong. Any pointers will be gratefully accepted!
Re: How do I remove columns after I've created TableViewer? [message #550719 is a reply to message #550635] Wed, 04 August 2010 08:15 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
The problem you have is that a table without a column is a legal construct.

Am 04.08.10 01:45, schrieb Jon Svede:
> Thanks! I have tried that, but I have this problem where no matter how
> I try to remove all the columns, 1 or more remain.
>
> For example, when I do the following:
>
>
> TableColumn[] columns = table.getColumns() ;
>
> for( TableColumn tc : columns ) {
> tc.dispose() ;
>

That's correct.

>
> I have 1 column left over with no header, just the data. Additionally,
> from the behavior it appears it starts with the last column and works
> backwards toward the first (right to left).
>
> When I try this:
>
>
> int x = table.getColumnCount() ;
> for( int y=0 ; y<x; y++ ) {
> table.getColumn(y).dispose() ;
> }
>


If you think about this you should grasp why this is not working and
giving you exceptions, right?

>
> It gives me an IndexOutOfBoundsException after the second (of four)
> column. Then if I get the count again, it says there are still two
> columns left and if I do another loop it will blow up after the first
> column - and again if I call table.getColumnCount() it says 1 is left,
> which I can dispose of but still there is 1 column of data visible.
> Very strange.
>
> Finally, if I do this:
>
>
> int x = table.getColumnCount() ;
> for( int y=x-1; y>-1; y-- ) {
> table.getColumn(y).dispose() ;
> }
>
>
> It leaves me with 1 column of data (the first one) with no header.
>
> I thought it might have to do with the indexing starting at 1 instead of
> 0 but it appears that the columns are zero based, so I don't think this
> is it either. If I change that last for loop to let y become negative
> it blows up.
>
> Clearly I am doing something wrong. Any pointers will be gratefully
> accepted!

You are not - as outlined in my first paragraph a Table without columns
is perfectly legal construct at the very moment you'll add one new
column the single one vanishes.

Beside that as outlined also in my initial response if you are removing
all columns anyways it's a lot better to dispose the complete Table and
recreate it from scratch.

Tom
Re: How do I remove columns after I've created TableViewer? [message #550847 is a reply to message #550719] Wed, 04 August 2010 13:46 Go to previous messageGo to next message
Jon Svede is currently offline Jon SvedeFriend
Messages: 83
Registered: July 2009
Member
Thanks, Tom.

>> int x = table.getColumnCount() ;
>> for( int y=0 ; y<x; y++ ) {
>> table.getColumn(y).dispose() ;
>> }
>>

>If you think about this you should grasp why this is not working and
>giving you exceptions, right?

My conclusion is that the number of columns diminishes while the index of the for loop grows. After disposing 2 columns the loop is looking to get the column at index=2 or column 3 which is beyond the size of the current column array. In all seriousness, thanks for making me think about it rather than just giving it up.

>Beside that as outlined also in my initial response if you are removing
>all columns anyways it's a lot better to dispose the complete Table and
>recreate it from scratch.

I'm obviously being obtuse because I can't seem to make that work (though I like that idea, it's cleaner).

I have an IPartListener listening for the close event from an editor. When I receive that event from the particular editor, I've tried the following:

  tableViewer.getTable().dispose() ;  // this causes issues


Instead, I've tried this:

  Table table = tableViewer.getTable() ;
   table.dispose() ;
   table = new Table( tableViewer.getControl().getParent(), SWT.NONE) ;


That doesn't work, but I think that it's related to the getControl() call since the underlying control has been disposed.

I've also tried caching the original Composite as a member variable and using it to re-create the TableViewer, but that doesn't seem to work either (though it doesn't throw any exceptions).

Any suggestions?


Thanks,

Jon
Re: How do I remove columns after I've created TableViewer? [message #550856 is a reply to message #550338] Wed, 04 August 2010 14:03 Go to previous messageGo to next message
Jon Svede is currently offline Jon SvedeFriend
Messages: 83
Registered: July 2009
Member
Here is something else that I just tried that is working:


  TableColumn[] columns = tableViewer.getTable().getColumns();

  for( TableColumn column : columns ) {
    column.dispose() ;
  }

  tableViewer.setInput( null ) ;

  tableViewer.refresh() ;



Again, this is in the IPartListener2.partClosed() method.

Is that right? I mean, it works but is it correct?

Thanks,

Jon

Re: How do I remove columns after I've created TableViewer? [message #550897 is a reply to message #550847] Wed, 04 August 2010 15:06 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Am 04.08.10 15:46, schrieb Jon Svede:
> Thanks, Tom.
>
>>> int x = table.getColumnCount() ;
>>> for( int y=0 ; y<x; y++ ) {
>>> table.getColumn(y).dispose() ;
>>> }
>>>
>
>> If you think about this you should grasp why this is not working and
>> giving you exceptions, right?
>
> My conclusion is that the number of columns diminishes while the index
> of the for loop grows. After disposing 2 columns the loop is looking to
> get the column at index=2 or column 3 which is beyond the size of the
> current column array. In all seriousness, thanks for making me think
> about it rather than just giving it up.
>> Beside that as outlined also in my initial response if you are removing
>> all columns anyways it's a lot better to dispose the complete Table and
>> recreate it from scratch.
>
> I'm obviously being obtuse because I can't seem to make that work
> (though I like that idea, it's cleaner).
> I have an IPartListener listening for the close event from an editor.
> When I receive that event from the particular editor, I've tried the
> following:
>
>
> tableViewer.getTable().dispose() ; // this causes issues
>
>
> Instead, I've tried this:
>

I think your code should look like this:

Table table = tableViewer.getTable();
Composite parent = table.getParent();
table.dispose();
TableViewer tableViewer = new TableViewer(parent);
// Create your columns
// Set ContentProvider
// Set Input
parent.layout(true); // You need to relayout

Tom
Re: How do I remove columns after I've created TableViewer? [message #550950 is a reply to message #550897] Wed, 04 August 2010 16:42 Go to previous messageGo to next message
Jon Svede is currently offline Jon SvedeFriend
Messages: 83
Registered: July 2009
Member
Thanks, Tom.

I don't know why it didn't occur to cache the Composite in the same method.

Your suggestion works (no surprises there, though) and I'll use that approach.

Jon
Re: How do I remove columns after I've created TableViewer? [message #1717274 is a reply to message #550950] Fri, 11 December 2015 11:06 Go to previous message
amriou smail is currently offline amriou smailFriend
Messages: 1
Registered: December 2015
Junior Member
You try to remove your colunm before you create it Laughing
that why your index of bound
Previous Topic:Remove finish button
Next Topic:ScrolledComposite and lack of method
Goto Forum:
  


Current Time: Sat Apr 20 01:43:18 GMT 2024

Powered by FUDForum. Page generated in 0.03986 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top