Still working through the table issues, but learning.
I am trying to implement removing a row from the table. I click on the row, then click on a button to remove the row. The action listener is called, and the model can be referenced, but when I issue the removeRow() method I get an Out Of Bounds exception, even though the row number being removed does exist. I just can't seem to see what I am doing wrong with this.
I've tried selecting the first and second rows, get the same errors regardless:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\pmll42>cd desktop\java\test
Maneesh Godbole wrote:Hint: After you remove the 1st selected row, think what happens to the row count.
I don't know. I put a println after the removeRow just to check that (I had the thought that I was looping and removing more than the two rows), the println was never executed. Apparently the exception is being thrown on the removeRow command itself.
Are you sure that you don't want to use an AbstractTableModel and not a DefaultTableModel here? The DTM comes with its own Vector of Vectors to hold the data, and as you are using it, currently this object is empty, so the DTM sees no rows. If in fact you want to use your own array as the model, then I think you're better off with the ATM.
I always recommend you start by using the DefaultTableModel because all the methods and functionality is implemented for you. Once you understand how models work then you can create a custom model if you feel it is necessary.
So start with the DefaultTableModel. The only method you need to override is the getColumnClass() method. You can create a DefaultTableModel by using a 2D array. The values in the Array will be copied to the DefaultTableModel so the model can reference the data. Now any changes to the data MUST be done by using the model.
When you delete multiple row you delete the last row first. That is instead of deleting rows 0, 1, 2, you delete rows 2, 1, 0. That way to don't try to delete a row that doesn't exist.
Joined: Sep 11, 2007
Thanks, this is going to take some re-working of the code, but I think you are right. Should have waded in rather than jumping into the deep end first.