简体   繁体   中英

How to refresh JTable after inserting data to database?

I'm populating JTable from access database. when running the code for the first time, table loads perfectly. Then adding new records to database from JDialog. What I was trying to do is to call loadData() method when JDialog is closed, but table is not updating.

This is my loadData() method:

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.

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.

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 . 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. My guess is that you just have to call the repaint() method so that the JTable gets updated as well.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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