• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deleting all the columns in Jtable  RSS feed

 
geeta khurana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have the results from a query and have dynamic no. of rows and columns. So, in order to populate the table, I need to delete all the previous rows and columns. Removing all the rows is fine but when it comes to removing all the columns from the table, I get the exception,
"java.lang.ArrayIndexOutOfBoundsException: 2 >= 2"
Can some one pls help me out of this.
Thanks.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Geeta,
It sounds like you are trying to remove to many columns. Keep in mind that in java indeices start with 0. That means:
2 columns
0, and 1 are the indices.
The error message is letting you know you should only remove columns 0 and 1 because 2 is not a valid column index.
Regards,
Manfred.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also remember that each column you remove the total columns is reduced by 1. So if you had 3 (0, 1, 2) to start, after removing the 1st you only have 2 (0 and 1). So you might want to check your loop.
 
geeta khurana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Manfred,
Thanks for ur reply. I'm using the folloing code to remove all the columns from the table:
for(int i=0;i<iColCnt;i++)<br /> {<br /> TableColumn TC = ivjtblResults.getColumnModel().getColumn(i);<br /> System.out.println("Removing col no. " + i);<br /> CM.removeColumn(TC);<br /> <br /> }<br /> iColCnt is the no. of columns in the table.<br /> I have 4 columns in the table initially and as soon as it reaches column 2 it gives the ArrayoutofBound exception 2>=2.
I tried doing with (iColCnt - 1) but still the same problem.

Thanks.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try something like this:
int numCols = ivjtblResults.getColumnCount();
while (ivjtblResults.getColumnCount() > 0) {
TableColumn TC = ivjtblResults.getColumnModel().getColumn(numCols - 1);
CM.removeColumn(TC);
numCols--;
}
You need to go from the end because as you remove a column the number of columns drops. This should give you the idea. It may need tweaked but should give the idea.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!