I am doing an application in Java using Swing. I have two tables, and I have to copy contents from one table to another (Replication.) The problem is, if I clear the destination Table
rows then my source table rows are also getting deleted.
If I press CopyAll
then I will copy all the contents from Table-A
to Table-B
.
If I press Clear
then I have to clear Table-B
. But the problem is Table-A
is also getting cleared.
For copying:
public void copyAll() {
TableModel tableAModel = tableA.getModel();
tableB.setModel(tableAModel);
repaint();
}
For clearing rows (I am doing for table-B
):
public void clearTableB() {
DefaultTableModel clearTableData = (DefaultTableModel) tableB.getModel();
clearTableData.setNumRows(0);
}
I think, I am getting problem while copying in copyAll()
method. I am getting tableA
's Model
and then clearing it at clearTable()
method.
If the above copyAll()
method is wrong, please, tell me how can I implement copyAll()
, removeTableB()
.
You have copied the TableModel
between the two tables. This means the two tables share the same data. If you delete the contents of the TableModel
, both tables will loose their data.
You should create two separate TableModel
instances, and keep them in sync (for example by using a listener as the TableModel
fires events each time the model is updated)