2

My JTable doesn't refresh after new data is entered into the database. I need to terminate and execute again to see results. I've included DefaultTableModel and fireTableDataChanged. What else did I miss? Thanks!

{
    columnNames = new Vector();
    data = new Vector(); 

    try{
        //database stuffs
    }

    catch{
    }  


    DefaultTableModel tm = new DefaultTableModel(); 
    JTable table = new JTable (tm); 
    JScrollPane scrollPane  = new JScrollPane(table);

    table = new JTable(data,columnNames)            
    {               
        public boolean isCellEditable(int row, int column){
            return false;
        }        

            public void newDataAvailable (TableModelEvent e){
            }
    };      

    scrollPane = new JScrollPane(table);            
    scrollPane.setBounds(33, 47, 256, 228);      
    panel.add(scrollPane);          

}

Can I create a 'Refresh Button' and refresh the values instead?

Lily S
  • 245
  • 1
  • 8
  • 18

3 Answers3

2

You shouldn't have to invoke fireTableDataChanged() explicitly; DefaultTableModel does this automatically when you update the model, as shown in this related example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks. I've removed fireTableDataChanged(), it's not working still. Am I supposed to add newDataAvailable() instead? Data is added directly into the database with PreparedStatement, is that affecting the automatic update? – Lily S May 02 '12 at 17:35
  • After you insert new data in the database and commit the result, you'll need run the original query again to fetch the new rows. – trashgod May 02 '12 at 19:59
2

You create new JTable instances. Create it just once and set the new model only to the existing JTable (added to container properly).

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Thanks. I amended my code but it does not seem to work still. Should I be invoking specific methods to refresh the values? Do I have to validate() or repaint() my JTable? – Lily S May 02 '12 at 18:42
0
  1. I have made a new project in that I have selected a new frame and drag a table, a label, a text field and a button from the swing controls to your frame.

2.create a table in back-end…

  1. Download rs2xml jar file from this link

Download rs2xml.jar

  1. Add it to the project library…

Right click on the project > properties > library > add Jar/folder > select jar > ok

5.Then connect mysql using following code

public class DBConnect {

Connection connection = null;

public Connection getConnection() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
        System.out.println("connection successfully");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return connection;
}
}
  1. Call the function in the page using following code

    con=new DBConnect();
    statement=con.getConnection().createStatement();
    

7.Then create a update function type the following code…

void update_table() throws SQLException{
    rs=statement.executeQuery("select * from details");
    jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}

8.In the buttons action event type the following and call the update function

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

   try {
       Object name=jTextField1.getText();
       String sql="insert into details (name) values('"+name+"')";
        int  resultset=statement.executeUpdate(sql);
        update_table();
   } catch (SQLException ex) {
       Logger.getLogger(update.class.getName()).log(Level.SEVERE, null, ex);
   }

}

9.Then run the project…

YOU can also Follow this Tutorial:--

How to refresh JTable after insert delete or update the data in java Netbeans ID

2sku
  • 31
  • 2