How to add new rows into a jTable from database while button click without clearing existing rows

后端 未结 1 1551
谎友^
谎友^ 2021-01-27 03:34

How to add new rows into a jTable from database while button click without clearing existing rows in jTable?

I tried lot of ways. But no success. Help

St         


        
相关标签:
1条回答
  • 2021-01-27 04:05

    Process each row of data from the ResultSet and create a Vector and use this method to insert the data into the table model. You are creating new table model and setting it on the table, the old model with the data is lost.

    After below request in comment:

    This is one way to do it.

    Vector<Vector<String>> data=new Vector<>();
    //Fill this Vector above with the initial data
    
    Vector<String> columns=new Vector<String>();
    //Fill this with column names
    
    DefaultTableModel tableModel=new DefaultTableModel(data, columns);
    JTable table=new JTable(tableModel);
    //Display the table as you like
    
    ... //Query the database and get the ResultSet (let's call it rs)
    
    while(rs.next){
    
      Vector<String> newRow=new Vector<>();
    
      //Get the data from the resultset and fill this new row
    
      tableModel.addRow(newRow);
    
    }//while closing
    
    0 讨论(0)
提交回复
热议问题