簡體   English   中英

刪除數據庫中的項目后刷新jtable

[英]Refresh jtable after Delete An item from database

從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);

所有你編碼的地方**Refresh code**看起來根本不需要。

查看DefualtTableModel以獲取更多方法,例如添加行等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM