This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Resetting Table data ... problem" Watch "Resetting Table data ... problem" New topic

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()];
vector= new Vector(Arrays.asList(tempAr));

Even then it has the current value.
Appreciate if anybody can help ...
Thanks in advance.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121


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



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

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

    subject: Resetting Table data ... problem
    Similar Threads
    Adding Column Dynamically to JTable
    Table Resetting data problem
    JTable issues
    JTable problem