This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable & AbstractTableModel(Delete row problem)

 
Faisal Ahmed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!!
I have used AbstractTableModel in my JTable.In case of multiple rows, i want to delete rows from interface as well as from database. As removeRow method is absent in AbstractTableModel.so DefaultTableModel has been used by casting but CastClassException throws. Here is Partial code(tried in many ways).Plz help me to solve the problem.
void jButtonCancel_actionPerformed(ActionEvent e) {
try{
jTable1.setModel(myModel); //jtable is object of JTable and myModel is of Abstract TableModel

DefaultTableModel model=((DefaultTableModel)jTable1.getModel());


int row=jTable1.getSelectedRow();
if(row>=0){
model.removeRow();
jTable1.revalidate();
model.fireTableDataChanged();
System.out.println("Rows Delete");
}
}
}
catch(Exception sq){
sq.printStackTrace();
sq.getMessage();
}

}
 
Bill Liteplo
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain how you create myModel.
AbstractTableModel is an abstract class. Therefore, you can't instantiate it directly.
Do you have your own TableModel class? I have a feeling you would find creating one to be useful. Then you can define the behavior you want custom to your problem (e.g. override deleteRow to also make a call to your database).
If DefaultTableModel does everything you need, then great.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic