• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Resetting Table data ... problem

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried using clone() on Vector ... still didn't work.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    reply
      Bookmark Topic Watch Topic
    • New Topic