Jtable and keeping track of updated rows
Hi
I have a situation where user can update multiple cells and then click update now button.
When the user clicks update now button, i will go through all the rows and compare the values with original values.
But i am having trouble retaining original values,
what i do is i clone the data vector to another vector, but when i update the table this new vector also gets updated, is there any of achieving what i am doing,
i have attached the code below for reference
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.awt.event.ActionEvent;
publicclass TestTableForChangedCellsextends JFrameimplements ActionListener
{
private JTable table;
private MyModel model;
private Vector allData, cloneData;
//private Object [] cloneData;
private JComboBox box;
public TestTableForChangedCells()
{
super("my Table");
buildData();
Container cont = this.getContentPane();
cont.setLayout(new BorderLayout());
cont.add(getTablePane(), BorderLayout.NORTH);
cont.add(getButtonPanel(), BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setSize(550,400);
this.show();
}
private JScrollPane getTablePane()
{
table = getTable();
JScrollPane scrollPane =new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(550, 310));
scrollPane
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
return scrollPane;
}
private JTable getTable()
{
Vector header =new Vector();
header.add("Notes");
header.add("Horizon");
header.add("Method");
model =new MyModel(allData, header);
JTable table =new JTable(model);
table.getTableHeader().setReorderingAllowed(false);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
for (int i = 0; i < model.getColumnCount(); i++)
{
TableColumn column1 = table.getColumnModel().getColumn(i);
switch (i)
{
case 0:
column1.setPreferredWidth(200);
break;
case 1:
column1.setPreferredWidth(75);
column1.setCellEditor(new DefaultCellEditor(new JTextField()));
break;
case 2:
column1.setPreferredWidth(200);
column1.setCellEditor(new DefaultCellEditor(box));
break;
}
}
table.setRowHeight(20);
return table;
}
private JPanel getButtonPanel()
{
JPanel buttonPanel =new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
buttonPanel.setPreferredSize(new Dimension(550, 30));
JButton addButton =new JButton("Add Row");
addButton.addActionListener(this);
buttonPanel.add(addButton);
return buttonPanel;
}
privatevoid buildData()
{
allData =new Vector();
Vector data =new Vector();
data.add("This is note one");
data.add("3");
data.add("SD");
Vector data1 =new Vector();
data1.add("This is note two");
data1.add("4");
data1.add("ID");
allData.add(data);
allData.add(data1);
cloneData =new Vector();
for(int i =0, size = allData.size(); i < size; i++)
{
cloneData.add(allData.get(i));
}
Vector comboData =new Vector();
comboData.add("PD");
comboData.add("RD");
comboData.add("SD");
comboData.add("ID");
box =new JComboBox(comboData);
}
publicstaticvoid main(String args[])
{
new TestTableForChangedCells();
}
publicvoid actionPerformed(ActionEvent parm1)
{
for(int i=0, size = cloneData.size(); i< size; i++)
{
Vector data = (Vector)cloneData.get(i);
Vector data1 = (Vector)allData.get(i);
System.out.println ("Clone Data Line "+i +" " + data.get(0) +" horizon " + data.get(1) +" method " + data.get(2) );
System.out.println ("Original Data Line "+i +" " + data1.get(0) +" horizon " + data1.get(1) +" method " + data1.get(2) );
}
}
}
class MyModelextends DefaultTableModel
{
public MyModel(Vector allData, Vector header)
{
super(allData, header);
}
public Object getValueAt(int row,int col)
{
// if (row==0)
//return prop_names[row];
return super.getValueAt(row,col);
}
publicboolean isCellEditable(int row,int col)
{
if (col==0)
{
returnfalse;
}
returntrue;
}
}

