We have a simple project where we read data from a socket and we want to populate a table with the coming data, but we can\'t find a way to add rows to a yet created J
Once you start dynamically adding and removing elements from a JTable, you really need to start using a TableModel.
See the Java Tutorial for more details.
You should create a custom TableModel. A JTable
doesn't actually store the rows, it always delegates that to a TableModel
. To help you implementing it, you should make use of AbstractTableModel. Don't forget to call fireTableRowsInserted() every time you add rows. For better performances if you add a lot of rows, try to batch the updates and add many rows at a time.
If you use the default table model for a JTable then you can add rows with following code
if ( dest+1 < table.getRowCount()-1 )
( (DefaultTableModel) table.getModel() ).insertRow(dest+1, getValuesForNewRow());
else
( (DefaultTableModel) table.getModel() ).addRow(getValuesForNewRow());