• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayOutOfBounds exception after removing column from model - not obvious to me why

 
Carvel Baus
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

I have code working (sorta) that allows me to add/remove a column from the model and I see the column disappear or reappear in the table. The model appears to be getting confused about column indexes.

For example, I have these columns:

View Index: 0 1 2 3 4 5 6
Model Index: 0 1 2 3 4 5 6

I tell the model to remove view columns 4 and 5 (yes, I am converting to proper model index before removing). Here is what it looks like now:

View Index: 0 1 2 3 4
Model Index: 0 1 2 3 6

When I convert the view index to the model index for view column "4" I get back 6. When I attempt to retrieve this column using model.getColumn(), I get an array out of bounds exception stating that 6 >= 5 (where 5 I think is the number of columns in the model.) It doesn't appear to be adjusting the index of columns higher up in the array after the remove.

To do the remove, I am calling: table.getColumnModel().remove(column) where column is the actual column object. I store this object in a hashmap elsewhere so I can add it back later.

Is there something I need to call to tell the model to fix itself after doing the remove?

Thanks
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to use a wrapper for the value of the index, instead of using an int use an Integer so instead of using 6 use new Integer(6)

(peace)
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carvel Baus wrote:
View Index: 0 1 2 3 4
Model Index: 0 1 2 3 6

When I convert the view index to the model index for view column "4" I get back 6. When I attempt to retrieve this column using model.getColumn(), I get an array out of bounds exception stating that 6 >= 5 (where 5 I think is the number of columns in the model.) It doesn't appear to be adjusting the index of columns higher up in the array after the remove.

To do the remove, I am calling: table.getColumnModel().remove(column) where column is the actual column object.


Perhaps I don't understand what you're asking, but this is as it should be.

The argument to TableColumnModel.getColumn() is what you call the view index, no?

In your case, model.getCoulmn(4).getModelIndex() will return 6.
 
Carvel Baus
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do get 6 back - then when I ask the model itself to provide the column based on that index (a TableColumn object,) it gives me the exception. So somewhere it thinks there is something at index 6 and somewhere it thinks there is nothing there. The model appears to be out of sync internally.
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carvel Baus wrote:I do get 6 back - then when I ask the model itself to provide the column based on that index (a TableColumn object,) it gives me the exception. So somewhere it thinks there is something at index 6 and somewhere it thinks there is nothing there. The model appears to be out of sync internally.


I don't get it.

If you agree that model.getCoulmn(4).getModelIndex() should return 6 then what do you expect model.getCoulmn(6).getModelIndex() to return?

I would expect model.getCoulmn(6) to throw an exception because 6 is not a valid view index, but you seem unhappy with that.
 
Carvel Baus
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not doing what I may have led you to believe I am doing. I am asking the model:"For view index of 4, what is the model index associated with that?" - it tells me "oh yes, that is index 6 in the model." When I ask for the column at that index, it says "hold the phone jack, ain't no column there."

Here is the code:

// determine column number in model - different from view if columns in view were moved or changed.
int modelColumnNum = component.getTable().convertColumnIndexToModel(viewColumnNum); // NOTE: viewColumnNum is 4 in my example.
System.out.println(viewColumnNum + ":" + modelColumnNum); // prints "4:6"

TableColumnModel tableModel = component.getTable().getColumnModel();
TableColumn column = tableModel.getColumn(modelColumnNum); // ***exception thrown here *****
String columnName = column.getHeaderValue().toString();


So, is the TableModel.getColumn(index) method asking for the view index, or the model index? I would think it wants the model index. Am I off here? The docs don't explicitly say.
 
Carvel Baus
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops - in my above statement I am actually asking the table (as the code shows) and not the model to get the model index from the view index.
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carvel Baus wrote:
So, is the TableModel.getColumn(index) method asking for the view index, or the model index? I would think it wants the model index.


If you reread my responses, you will see that I have twice mentioned that it wants the view index.

I agree that it's not documented especially well, but it does make sense. DefaultTableColumnModel keeps a Vector of TableColumns in view order, and its getColumn() method simply performs a get() on the Vector.
 
Carvel Baus
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian,

That did the trick and I don't see anyway to have figured that out from the docs unless one is intimately familiar with the implementation - that stuff could really be documented better.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!