//删除JTable表中选中记录。
DefaultTableModel dtm=(DefaultTableModel)this.jTable1.getModel();
int[] row = this.jTable1.getSelectedRows();
for(int i=0;i<row.length;i++)
{
dtm.removeRow(row[i]-i);//这儿要减i wrc
}
DefaultTableModel dtm=(DefaultTableModel)this.jTable1.getModel();
int[] row = this.jTable1.getSelectedRows();
for(int i=0;i<row.length;i++)
{
dtm.removeRow(row[i]-i);//这儿要减i wrc
}
int n=JOptionPane.showOptionDialog(this,"确定删除该餐桌信息吗?", "删除确认",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
if(n==0){
Connection conn = Connect.Connect();
int RowSel[] = DishTable.getSelectedRows();
String RowSelStr[] = new String[RowSel.length];
for(int s = RowSel.length -1;s >= 0 ;s--){
RowSelStr[s] = (String)modelc.getValueAt(RowSel[s],0);
modelc.removeRow(RowSel[s]);
}
for(int i = 0;i<RowSel.length;i++){
RowSelStr[i] = "Delete from menus where name = '"+RowSelStr[i]+"'";
//new DesksTask(conn).Delete(RowSelStr);
try {
new ForeMarg(conn).Delete(RowSelStr[i]);
} catch(Exception err) {
err.printStackTrace();
}
}
if(modelc.getRowCount() != 0){
DishTable.setRowSelectionInterval(RowSel[0],RowSel[0]);
}
JOptionPane.showMessageDialog(this,"删除成功","删除",JOptionPane.INFORMATION_MESSAGE);
}else{
JOptionPane.showMessageDialog(this,"删除失败","请查看",JOptionPane.INFORMATION_MESSAGE);
}
对于Model,删除的话,会更新,也就是说 从 0~n 这样删除是不对的,和实际删除的数据不一致。应该从 n~0 删除,这样Model变化不会影响到记录的Index
DefaultTableModel dtm=(DefaultTableModel)this.jTable1.getModel();
int[] row = this.jTable1.getSelectedRows();
for(int i=row.length-1; i >=0; i--) {
dtm.removeRow(row[i]);
}
呵呵,一家之言.