This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Resetting Table data ... problem

 
Seema Hanji
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SeemaH,

Please re-register with a name that follows the JavaRanch Official UserName Policy. It states that user names have to be in the form "First Name" + space + "Last Name".

In answer to your question... Try to use the clone() method of Vector to get a "non-linked" copy...

Thanks,

-Nate
 
Seema Hanji
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using clone() on Vector ... still didn't work.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

    -Nate
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic