aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable can't delete row Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable can Watch "JTable can New topic
Author

JTable can't delete row

Dean Reedy
Ranch Hand

Joined: Sep 10, 2001
Posts: 89
I can not get my JTable to delete a row, but it does print the print line "Deleted by AbstractTableModel Table Model". Here is the code I am using:

public void deleteRow(int Row) {
fireTableRowsDeleted(Row, 0);
fireTableStructureChanged();
System.out.println("Deleted by AbstractTableModel Table Model");
}
I have another function which works fine deleting all of the rows:
public void removeData() {
m_vector.removeAllElements();
fireTableStructureChanged();
}
I have also tried using the:
m_vector.removeElementAt(Row);
Jennifer Garrett
Greenhorn

Joined: Oct 31, 2001
Posts: 13
What if you change the deleteRow method to this:
public void removeRow(int rowNumber) {
data.removeElementAt(rowNumber);
fireTableRowsDeleted(rowNumber, rowNumber);
}
The data variable is a vector of vectors containing all the data in the table cells.
Dean Reedy
Ranch Hand

Joined: Sep 10, 2001
Posts: 89
Nope, that didn't work!
Jennifer Garrett
Greenhorn

Joined: Oct 31, 2001
Posts: 13
hmm, what do you do in the class that instantiates your subclass of AbstractTableModel? Do you have a listener to wait for some action indicating you want to delete a row and then call the method (either removeRow or deleteRow, whichever you use in the class that extends AbstractTableModel).
tModel.removeRow(tableIndex);
tModel is the instance of the class that extends AbstractTableModel, and tableIndex is the row number to delete.
Dean Reedy
Ranch Hand

Joined: Sep 10, 2001
Posts: 89
This is actually the funcvtion used in the class which extends the abstractmodel:
public void deleteRow(int Row) {
fireTableRowsDeleted(Row, 0);
fireTableStructureChanged();
System.out.println("Deleted by AbstractTableModel Table Model");
}
Now I call it simple by:
m_table.deleteRow(somevalue);
Is this the questions you are asking? I am able to call the following function, the same way which deletes all of the rows.
public void removeData() {
m_vector.removeAllElements();
fireTableStructureChanged();
}
I could send you all of the code but quite long.
Thanks for you time.
Jennifer Garrett
Greenhorn

Joined: Oct 31, 2001
Posts: 13
change this line
fireTableRowsDeleted(Row, 0);
to:
fireTableRowsDeleted(Row, Row);
I think the fireTableRowsDeleted method expects the first argument to be the first row to delete, and the second argument is the last row to delete, and it deletes all the rows between the first argument and the second argument.
Dean Reedy
Ranch Hand

Joined: Sep 10, 2001
Posts: 89
I solved it! Thanks very much for your help!
here is the code that works.
public deleteRow(int Row) {
m_vector.remove(Row);
fireTableStructureChanged();
System.out.println("Deleted by AbstractTableModel Table Model" + Row);
return ideleteRow;
}
With my head hanging low, my mistake was that I was passing in the value of the first column of the row I wanted to delete, and not the index. By printing the row value I saw my mistake.
Thanks and sorry to waste your time.
Dean
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable can't delete row