Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable can't delete row

 
Dean Reedy
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, that didn't work!
 
Jennifer Garrett
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic