File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Resetting Table data ... problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Resetting Table data ... problem" Watch "Resetting Table data ... problem" New topic
Author

Resetting Table data ... problem

Seema Hanji
Ranch Hand

Joined: Jan 29, 2001
Posts: 37
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

Joined: Oct 18, 2000
Posts: 4121

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


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Seema Hanji
Ranch Hand

Joined: Jan 29, 2001
Posts: 37
I tried using clone() on Vector ... still didn't work.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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
     
    wood burning stoves
     
    subject: Resetting Table data ... problem