简体   繁体   English

将数据插入数据库后如何刷新JTable?

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

I'm populating JTable from access database. 我正在从访问数据库填充JTable。 when running the code for the first time, table loads perfectly. 首次运行代码时,表可以完美加载。 Then adding new records to database from JDialog. 然后从JDialog将新记录添加到数据库。 What I was trying to do is to call loadData() method when JDialog is closed, but table is not updating. 我试图做的是在JDialog关闭但表未更新时调用loadData()方法。

This is my loadData() method: 这是我的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);
}`

this how I call loadData method when closing the JDialog. 这是我在关闭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); 

My database is updated when adding the records but Jtable is not refreshed. 添加记录时,我的数据库已更新,但Jtable未刷新。

Here: 这里:

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

Don't re-create the table but update its model instead, either by setting a new table model or by clearing and re-filling the current one: 不要重新创建表,而是通过设置新的表模型或清除并重新填充当前表模型来更新其模型:

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

See examples here (includes SwingWorker to make database calls in a background thread), here and here . 见的例子在这里 (包括SwingWorker的做在后台线程的数据库调用), 在这里这里 Please have a look to those answers, there are explanations to make the point clear. 请看一下这些答案,这里有一些解释可以使您明白这一点。

This is a shot in the dark, but maybe this will work?: 这是在黑暗中拍摄的镜头,但是也许可以吗?:

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

If I understand what is going on, the underlying database is getting updated but the JTable component is not showing the updates. 如果我了解发生了什么,则底层数据库正在更新,但是JTable组件未显示更新。 My guess is that you just have to call the repaint() method so that the JTable gets updated as well. 我的猜测是,您只需要调用repaint()方法即可使JTable也得到更新。

This worked for me: 这为我工作:

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

 setTablevalue();

I removed all the rows from the JTable and again called the setTableValue method to re-populate the table. 我从JTable删除了所有行,然后再次调用setTableValue方法来重新填充表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM