【发布时间】:2014-03-14 19:08:35
【问题描述】:
从 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
如果你知道这个我不知道为什么会发生这个错误,请在这里分享..
【问题讨论】:
-
您是否尝试删除整个表并创建另一个然后将其添加回来?
-
另外你到底想删除什么?只是一个单元格,还是一行?
-
@peeskillet 我试图从表中删除一个项目,该表删除了删除项目...
-
@peeskillet 一行。