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
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