0
我想序列化和反序列化JTable中的數據稱爲Ex,我得到一個行爲與Jtable和DefaultTableModel我不明白。 我使用List<String[]>
作爲數據容器,我不確定如何反序列化。序列化和反序列化列表<String[]> JTable
public class Ex implements Serializable {
static List<String> S1 = new ArrayList<String>();
static List<String[]> Z1 = new ArrayList<String[]>();
...
Ex
{
for (int i = 0; i < 1; i++) {Z1.add(new String[] {(i+1)+"",((i+1)*10)+"",((i+1)*100)+""});}
data = new DefaultTableModel(Z1.toArray(new Object[][] {}), S1.toArray());
tab = new JTable(data);
...
}
}
序列化和反序列化方法使用按鈕叫這個樣子
public void Serializer(Ex caller)
{
try {
FileOutputStream fs = new FileOutputStream("Ser.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(caller);
os.flush();
os.close();
} catch (Exception e) {e.printStackTrace(); }
}
public void Deserializer()
{
Ex J = null;
try {
FileInputStream fis = new FileInputStream("Ser.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
J = (Ex) ois.readObject();
ois.close();
} catch (Exception e) { e.printStackTrace(); }
DefaultTableModel data2 = new DefaultTableModel(J.Z1.toArray(new Object[][] {}), J.S1.toArray());
tab.setModel(data2);
}
MyTableListener看起來是這樣的:
public class MyTableListener implements TableModelListener
{
Ex x;
MyTableListener(Ex E) {x=E;}
public void tableChanged(TableModelEvent e)
{ int row = e.getLastRow();
int col = e.getColumn();
String[] rowBuffer = x.Z1.get(row);
rowBuffer[col]=(String)x.data.getValueAt(row,col);
}
}
,並在主要的方法:
...
Ex THIS = new Ex();
Tlistener = new MyTableListener(THIS);
data.addTableModelListener(Tlistener);
...
可以在請指定如何正確反序列化List<String[]>
?