簡體   English   中英

將數據插入數據庫后如何刷新JTable?

[英]How to refresh JTable after inserting data to database?

我正在從訪問數據庫填充JTable。 首次運行代碼時,表可以完美加載。 然后從JDialog將新記錄添加到數據庫。 我試圖做的是在JDialog關閉但表未更新時調用loadData()方法。

這是我的loadData()方法:

private void loadData() {
    System.out.println("sssss");
    final String [] columnNames={"Seq", "First Name", "Last Name","Num1","Num2","Num3"};
    connectDb();

    data = new Object[rows][columns];
    int row = 0;
    try {
        while(rs.next()){
            for(int col = 0 ; col<columns; col++ ){
                if(col==0)
                    data[row][col]=rs.getString("contact_seq");
                if(col==1)
                    data[row][col]=rs.getString("contact_fname");
                if(col==2)
                    data[row][col]=rs.getString("contact_lname");
                if(col==3)
                    data[row][col]=rs.getString("contact_num1");
                if(col==4)
                    data[row][col]=rs.getString("contact_num2");
                if(col==5)
                    data[row][col]=rs.getString("contact_num3");


            }
            row++;
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    model = new DefaultTableModel(data, columnNames){

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        public boolean isCellEditable(int row, int column)
        {
            return false;
        }



     };

     table = new JTable(model);
}`

這是我在關閉JDialog時如何調用loadData方法的方法。

JMenuItem mntmNew = new JMenuItem("New Contact");
    mntmNew.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            addData gui = new addData(viewData.this,rs);
            gui.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            gui.setVisible(true);
            gui.addWindowListener(new WindowAdapter() {
                public void windowClosed(WindowEvent e){
                    loadData();
                }
            });
        }
    });
    mnFile.add(mntmNew); 

添加記錄時,我的數據庫已更新,但Jtable未刷新。

這里:

private void loadData() {
    ...
    table = new JTable(model); // don't re-create the table here
}

不要重新創建表,而是通過設置新的表模型或清除並重新填充當前表模型來更新其模型:

private void loadData() {
    ...
    table.setModel(model); 
    // Of course, table should have been initialized
    // and placed first, and only once!
}

見的例子在這里 (包括SwingWorker的做在后台線程的數據庫調用), 在這里這里 請看一下這些答案,這里有一些解釋可以使您明白這一點。

這是在黑暗中拍攝的鏡頭,但是也許可以嗎?:

public void windowClosed(WindowEvent e) {
    loadData();
    // Add this line:
    table.repaint();
}

如果我了解發生了什么,則底層數據庫正在更新,但是JTable組件未顯示更新。 我的猜測是,您只需要調用repaint()方法即可使JTable也得到更新。

這為我工作:

 if (model.getRowCount() > 0) {
     for (int i = model.getRowCount() - 1; i > -1; i--) {
         model.removeRow(i);
     }
 }

 setTablevalue();

我從JTable刪除了所有行,然后再次調用setTableValue方法來重新填充表。

暫無
暫無

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

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