2

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().

Roman C
  • 49,761
  • 33
  • 66
  • 176
Amarnath
  • 8,736
  • 10
  • 54
  • 81

5 Answers5

4

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)

Robin
  • 36,233
  • 5
  • 47
  • 99
  • Thanks a lot. But how can I create a copy of the TableModel instead of using the Original once reference in copyAll() method? – Amarnath Aug 14 '12 at 08:27
  • Copying the data in the table model instead of the table model itself – Robin Aug 14 '12 at 08:32
  • So you mean to say, I have to go for each ValueAt(row, column) of tableA and then paste it at corresponding cell in tableB rt? Is that the only way? – Amarnath Aug 14 '12 at 08:39
  • 1
    @Amarnath yes, for making a copy you will have to iterate over all the values – Robin Aug 14 '12 at 09:02
3

In your copy version, you set the model of the first table to the second table. So the two tables share the same model. You should make a copy of the model :

public void copyAll() {
    final TableModel tableAModel = tableA.getModel();
    final DefaultTableModel copy = new DefaultTableModel(tableAModel.getRowCount(), 0);
    for (int column = 0; column < tableAModel.getColumnCount(); column++) {
        copy.addColumn(tableAModel.getColumnName(column));
        for (int row = 0; row < tableAModel.getRowCount(); row++)
            copy.setValueAt(tableAModel.getValueAt(row, column), row, column);
    }
    tableB.setModel(copy);
}
gontard
  • 28,720
  • 11
  • 94
  • 117
2

Both tables are using the same model. You have to give Table B it's own Model, copy the values manually. Your current copyAll method copies the reference to the Table Model, it doesn't copy the contents.

1

That is because you shared the TableModel for the two tables. In the copy method, you should create a clone of the Model and use the clone for the second table.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
1

If you are using DefaultTableModel You can get Vector of data from the model using getDataVector() and clone() it.

public void copyAll() {
   TableModel tableAModel = tableA.getModel(), tableModelB;
   Vector tableModelBDataVector = ((DefaultTableModel)tableAModel).getDataVector();
   int tableModelAColumnCount = tableAModel.getColumnCount();
   Vector<String> tableModelAColumnVector = new Vector<String>(tableModelAColumnCount);
   for (int i = 0; i < tableModelAColumnCount; i++)
     tableModelAColumnVector.add(tableAModel.getColumnName(i));
   tableModelB = new DefaultTableModel((Vector)tableModelBDataVector.clone(), (Vector)tableModelAColumnVector.clone());
   tableB.setModel(tableModelB);
}
Roman C
  • 49,761
  • 33
  • 66
  • 176