删除JTable的自动刷新不工作的项目...刷新JTable中后删除一个项目从数据库
这里是代码
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnEdit) {
} else if (e.getSource() == btnDelete) {
String str = JOptionPane.showInputDialog(null,
"Enter The Reason : ", "", 1);
if (str != null) {
Book updatebook = new Book();
updatebook.setName(book.getName());
updatebook.setAuthor(book.getAuthor());
updatebook.setPublisher(book.getPublisher());
updatebook.setDelete(true);
ServiceFactory.getBookServiceImpl().updateBook(updatebook);
JOptionPane.showMessageDialog(null, "You entered the Reason : "+ str, "", 1);
**Refresh code**
listPanel.removeAll();
listPanel.repaint();
listPanel.revalidate();
getBooks();
getListBookPanel();
booktable.repaint();
booktable.revalidate();
} else
JOptionPane.showMessageDialog(null,
"You pressed cancel button.", "", 1);
}
}
getBooks()函数
public JTable getBooks() {
booktable = new JTable();
String[] colName = { "Name", "Author ",
"Publisher" };
List<Book> books = ServiceFactory.getBookServiceImpl().findAllBook();
data = new Object[books.size()][100000];
for (Book book : books) {
data[i][0] = book.getName();
data[i][1] = book.getAuthor();
data[i][2] = book.getPublisher();
i++;
}
DefaultTableModel dtm = new DefaultTableModel(data, colName);
booktable = new JTable();
booktable.setModel(dtm);
dtm.fireTableDataChanged();
booktable.setCellSelectionEnabled(true);
booktable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int row = booktable.getSelectedRow();
CallNo = (booktable.getValueAt(row, 0).toString());
}
});
return booktable;
}
错误
“AWT-EventQueue的 - 0” java.lang.ArrayIndexOutOfBoundsException:2
我不知道为什么,如果u知道这件事,请在这里分享这个错误发生..
您是否尝试删除整个表格并创建另一个表格然后将其添加回来? –
你究竟想要移除什么?只是一个单元格,还是一排? –
@peeskillet我试图从表格中删除一个项目,表格删除删除项目... –