gpt4 book ai didi

java - 从数据库中删除项目后刷新 jtable

转载 作者:行者123 更新时间:2023-12-02 06:12:05 25 4
gpt4 key购买 nike

从 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

我不知道为什么会发生这个错误,如果你知道这一点,请在这里分享..

最佳答案

您尝试删除数据的方式似乎效率低下,而且不正确。看起来您正在尝试使用代码执行的操作是创建一个完整的其他表并将其替换为新表。不要那样做。只需更新 TableModel 即可。你可以直接使用它的方法

只需使用此方法,就会自动从表中删除一行。所以你可以在监听器的某个地方做这样的事情

DefaultTableModel model = (DefaultTableModel)bookTable.getModel();
int row = ...
model.removeRow(row);

您在**刷新代码**处编写的所有代码看起来根本没有必要。

查看DefualtTableModel了解更多方法,例如添加行等。

关于java - 从数据库中删除项目后刷新 jtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21796021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com