Hi All, I have a JTable with DefaultTableModel as model. When I first populate table with data(from database), I am storing the table data locally in a vector, using Vector cols; //this is initialized with column names. Vector data = ((DefaultTableModel)table.getModel()).getDataVector(); This table is editable, when user changes data and hits save, if any problem occurs while saving, I want to reset table data to previous values . So, I am trying to reset table data as below. ((DefaultTableModel)table.getModel()).setDataVector(data, cols); But it does't work, the vector data now has the current values that user has modified to, not the previously stored values.
I also tried to store data by doing a copy of the data as below, Vector tempV; tempV = ((DefaultTableModel)table.getModel()).getDataVector(); Object  tempAr = new Object[tempV.size()]; tempV.copyInto(tempAr); vector= new Vector(Arrays.asList(tempAr));
Even then it has the current value. Appreciate if anybody can help ... Thanks in advance. -Ssh
The problem is that the reference to the Vector is being changed, but the new Vector is still pointing to the same objects the old one is pointing to. So when data in the objects change, they update in both Vectors since they both still hold references to the same objects... This is basically a question of shallow vs. deep copying. You need to make a method that creates a way to do deep copying of your data... it would look something like this...
create a new Vector.
Loop through all objects in the old Vector.
For each object in the old Vector, create a new object of that type.
Set the data of the new object to be the same as the old object.
If any of the data in your old object is a class reference (i.e. anything except primitive types and Strings...) be prepared to create a new verion of those to add to the new object.
You may also want to do a search on "Java deep copy" or "Java deep clone" on a search engine such as Google... There's probably some pre-built code out there to do this pretty generically...