publicvoidactionPerformed(ActionEventarg0){int[]rows=table.getSelectedRows();//取得表格行数for(inti=0;i
public void actionPerformed(ActionEvent arg0) {
int[] rows = table.getSelectedRows();
//取得表格行数
for (int i = 0; i < rows.length; i++) {
rows[i] = table.convertRowIndexToModel(rows[i]);
}
if (rows.length < 1) {
JOptionPane.showMessageDialog(null, (Object) "请选择行后再删除!");
} else {
try {
int id = Integer.parseInt(table.getValueAt(
table.getSelectedRow(), 0).toString());// 获取删除行的图书id
int type = JOptionPane
.showConfirmDialog(null, "您将要删除 " + rows.length
+ " 行!删除后数据将不可恢复,你真的要删除吗?", "确认删除",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
BooksDao db = new BooksDao();
if (type == JOptionPane.YES_OPTION) {
for (int i = rows.length; i > 0; i--) // 注意这个地方要从后往前删
{
id = Integer.parseInt(table.getValueAt(
table.getSelectedRow(), 0).toString());
// 每次删除一行后,getSelectedRow得到的就是下一行的,之后在进行删除。
if (db.delBookes(id)) {
JOptionPane.showMessageDialog(null, "删除成功!");
((DefaultTableModel) table.getModel())
.removeRow(table.getSelectedRow());
} else {
JOptionPane.showMessageDialog(null, "删除失败!");
}
System.out.println("id: " + id);
}
}
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, (Object) "所选行为无效行!");
}
}
}
}
问题
其中::
int id = Integer.parseInt(table.getValueAt(
table.getSelectedRow(), 0).toString());// 获取删除行的图书id
为什么后面是toString前面是int
展开