JTable inserts too many rows
843806Jun 12 2009 — edited Jun 15 2009Hello all again,
I am getting this very time consuming error when I insert rows into my JTable. Everytime I insert a new row into the DefaultTableModel for this jtable, it will add an extra blank row at the bottom. And always, when I delete from the JTable, it will still leave more rows visible than should be on the jtable. This is how my code is set up:
I create a string array of my column headers,
String colHeader [] = new String[]{"1", "2", "3"};
DefaultTableModel dfltTblMdl = new DefaultTableModel(null, colHeader);
JTable jtbl = new JTable(dfltTblMdl);
//Now later in my code, I can press a Refresh button or when new sockets connect to my program, their connection information is displayed into the jtable as follows:
this.dfltTblMdl.setRowCount(numConnectedSockets);
for(int i = 0; i < numConnectedSockets; i++)
{
this.dfltTblMdl.insertRow(i,(new Object[]{"etc"...}));
}
The insert is working fine, it's placing the new information into the default table model which is showing up in my JTable, however, I am also getting blank rows that should not be displayed on the JTable. Further, when an socket disconnects, I call a refresh method to remove everything from the default model and repopulate again, however, some of the data from a previous but non-existent connection lingers on the JTable.
I've tried the fireTableRowsUpdated methods, fireTableRowsInserted, fireTableDataChanged methods, but non of these seem to get rid of the extra rows placed at the bottom when a new connection is received, nor eliminate the old data from the jtable when I refresh everything.
I am thinking perhaps is has something to do with data stored in the DefaultTableModel...but I'm not sure. Thanks soooooooooooo much in advance for your expertise in this matter!