以下代码不完整,仅把关键的代码贴出来
String userTitle[] = {"ID","用户名","密码","邮箱","名字","等级"};
tab = new Object[userInfoNumber][6];
Iterator<User> iter = userList.iterator();
while(iter.hasNext()){
User user = iter.next();
tab[i][0] = user.getId();
tab[i][1] = user.getUserName();
tab[i][2] = user.getPwd();
tab[i][3] = user.getMail();
tab[i][4] = user.getName();
whatGrade = user.getGrade() == 1 ? "管理员" : "普通会员";
tab[i][5] = whatGrade;
i ++;
}
DefaultTableModel tableModel = new DefaultTableModel(tab,userTitle);
JTable table = new JTable(tableModel);
for(int j = 0;j < table.getRowCount();j ++){
//获得删除时选择的ID在JTABLE中的行号
if(Integer.parseInt(table.getValueAt(j, 0).toString()) == id){
int selIndex = j;
if( selIndex < 0 || selIndex >=table.getRowCount() ){
return ;
}
//删除这一行
tableModel.removeRow(selIndex) ;
//刷新
tableModel.fireTableDataChanged();
break;
}
}
如果是更新数据的话,只需
tableModel.fireTableDataChanged();