(我不确定标题,因为我不知道如何清楚地表达我的想法)。
我有一个JTable。每次用户想要改变它的一些数据时,我都希望将旧数据保存在List中。然后我想做一些sql更新的东西,所以我想知道发生了更改的确切行。因此,我决定使用特定行作为键创建一个Map,使用旧数据创建一个列表(Map>)。
下面是我的代码
public class MapListsInJtable extends JFrame{
private final Object[][] rowdata = { {"one",1}, {"two",2}, {"three",3}};
private final Object[] colnames = {"col1", "col2", "col3"};
private final List<Object> oldValues; //for updated list
private final Map<Integer,List<Object>> updatedList;
private DefaultTableModel model;
private JTable table;
private JScrollPane scrollPane;
public MapListsInJtable(){
this.updatedList = new Hashtable<>();
this.oldValues = new ArrayList<>();
initializeTable();
}
private void initializeTable(){
model = new DefaultTableModel(rowdata,colnames);
table = new JTable(model){
@Override
public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
super.changeSelection(rowIndex, columnIndex, toggle, extend);
if(toggle){
//user gathers rows in order to delete them by holding ctrl
System.out.println("toggling...");
for(int i = 0; i < model.getColumnCount(); i++)
oldValues.add(table.getValueAt(rowIndex, i));
}
else{
//user presses left mouse button and selects multiple rows
if(extend){
System.out.println("extends...");
for(int i = 0; i < model.getColumnCount(); i++)
oldValues.add(table.getValueAt(rowIndex, i));
}
else{
oldValues.clear();
}
}
}
};
//to see if there has been change in table data
ListSelectionHandler handler = new ListSelectionHandler(this);
table.getSelectionModel().addListSelectionListener(handler);
table.setCellSelectionEnabled(false);
table.setCellSelectionEnabled(false);
table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(true);
//resize columns and use horizontal scroll bar to view data
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//disable column dragging
table.getTableHeader().setReorderingAllowed(false);
scrollPane = new javax.swing.JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
protected DefaultTableModel getModel(){return model;}
public JTable getTable(){return table;}
public List<Object> getOldValues(){return this.oldValues;}
public Map<Integer,List<Object>> getUpdatedList(){return this.updatedList;}
public void initializeUI(){
getContentPane().add(scrollPane);
setSize(300,150);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
private class ListSelectionHandler implements ListSelectionListener{
private final MapListsInJtable m;
public ListSelectionHandler(MapListsInJtable m){this.m = m;}
@Override
public void valueChanged(ListSelectionEvent e) {
//in order not valueChanged to be called twice
boolean hasBeenAdjusted = e.getValueIsAdjusting();
if(hasBeenAdjusted == true){
int selectedRow = m.getTable().getSelectedRow();
//put data in updatedList
for(int i = 0; i < m.getModel().getColumnCount(); i++)
m.getOldValues().add(m.getTable().getValueAt(selectedRow, i));
m.getUpdatedList().put(selectedRow, m.getOldValues());
System.out.println("updatedList: " + m.getUpdatedList());
}
}
}
public static void main(String[] af){
MapListsInJtable m = new MapListsInJtable();
m.initializeUI();
}
}
当我选择一行时,数据通常放在updatedList中。
输出:
updatedList: {0=[`one, 1, null`]}
但是当我选择另一行时,我在updatedList中有2个位置和新行的数据。之前的所有数据都已消失。
输出:
updatedList: {1=[`two, 2, null`], 0=[`two, 2, null`]}
有人可以解释一下,为什么会这样?我该怎么办呢?提前谢谢
答案 0 :(得分:1)
我认为这段代码就是问题:
int selectedRow = m.getTable().getSelectedRow();
//put data in updatedList
for(int i = 0; i < m.getModel().getColumnCount(); i++)
m.getOldValues().add(m.getTable().getValueAt(selectedRow, i));
m.getUpdatedList().put(selectedRow, m.getOldValues());
System.out.println("updatedList: " + m.getUpdatedList());
}
所以,试试这个:
int selectedRow = m.getTable().getSelectedRow();
//put data in updatedList
for(int i = 0; i < m.getModel().getColumnCount(); i++)
m.getOldValues().add(m.getTable().getValueAt(selectedRow, i));
List oldvalue = ((List) ((ArrayList) m.getOldValues()).clone());
m.getUpdatedList().put(selectedRow, oldvalue);
System.out.println("updatedList: " + m.getUpdatedList());
}