gpt4 book ai didi

java - 数据未加载回 Jtable(使用可序列化对象)

转载 作者:行者123 更新时间:2023-12-01 12:47:24 25 4
gpt4 key购买 nike

我有一个 Jtable 上显示的对象数组列表。对于 Jtable,我实现了一个扩展 AbstractTableModel 的模型。我希望用户能够保存和加载 .txt 文件。目前,我可以创建并保存文件,但是当我尝试将数据加载回 jTable 时,没有任何反应。
请看我的代码。如有任何建议,我们将不胜感激!
谢谢!

模型

package cartedetelefon;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

public class CarteDeTelefon extends AbstractTableModel implements Serializable {

private static List<Abonat> listaContacte = new ArrayList<Abonat>();
public static File f;
private static final long serialVersionUID = 1L;
private static final Logger fLogger = Logger.getLogger(CarteDeTelefon.class.getPackage().getName());

/**
* @return the listaContacte
*/
public static List<Abonat> getListaContacte() {
return listaContacte;
}

/**
* @param aListaContacte the listaContacte to set
*/
public static void setListaContacte(List<Abonat> aListaContacte) {
listaContacte = aListaContacte;
}

private final String[] numeColoane = {
"Nume",
"Prenume",
"CNP",
"Numar telefon"
};

// add contact to list
public void adaugareContact(String nume, String prenume, String cnp, String tel) {

try {
Long s = Long.valueOf(tel);
getListaContacte().add(new Abonat(nume, prenume, cnp, new NrTel(s)));

fireTableDataChanged();
} catch (NumberFormatException numberFormatException) {
}

}

@Override
public int getRowCount() {
if (getListaContacte().size() <= 0) {
return 0;
} else {
return getListaContacte().size();
}

}

@Override
public int getColumnCount() {
return numeColoane.length;
}

@Override
public String getColumnName(int col) {
return numeColoane[col];
}

@Override
public Object getValueAt(int row, int col) {
if (col == 0) {
return getListaContacte().get(row).getNume();
} else if (col == 1) {
return getListaContacte().get(row).getPrenume();
} else if (col == 2) {
return getListaContacte().get(row).getCNP();
} else if (col == 3) {
return getListaContacte().get(row).getNrTel();
}

return "Eroare";
}

@Override
public void setValueAt(Object aValue, int rowIndex, int colIndex) {

Abonat abonat = getListaContacte().get(rowIndex);
switch (colIndex) {
case 2:
abonat.setCNP((String) aValue);
break;
case 3:
abonat.setNrTel((NrTel) aValue);
break;
}
fireTableRowsUpdated(rowIndex, rowIndex);

}

@Override
public boolean isCellEditable(int row, int colNum) {
switch (colNum) {
case 2:
return false;
default:
return true;
}
}

//save contacts
public void salvareContacte() throws FileNotFoundException, IOException {

try (
OutputStream file = new FileOutputStream("contacts.txt");
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = new ObjectOutputStream(buffer);) {
output.writeObject(listaContacte);
} catch (IOException ex) {
fLogger.log(Level.SEVERE, "Cannot perform output.", ex);
}

}

//load contacts
public void incarcareContacte() throws IOException, ClassNotFoundException {

try (
InputStream file = new FileInputStream("contacts.txt");
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream(buffer);) {
//deserialize the List
List<Abonat> recoveredContacts = (ArrayList<Abonat>) input.readObject();
//display its data
for (Abonat contacts : recoveredContacts) {
System.out.println("Recovered contacts: " + contacts);
}
} catch (ClassNotFoundException ex) {
fLogger.log(Level.SEVERE, "Cannot perform input. Class not found.", ex);
} catch (IOException ex) {
fLogger.log(Level.SEVERE, "Cannot perform input.", ex);
}
}

}

保存按钮

private void saveActionPerformed(java.awt.event.ActionEvent evt) { 



try {
model.salvareContacte();
} catch (IOException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}

}

打开按钮

private void openActionPerformed(java.awt.event.ActionEvent evt) {   



final JFileChooser fc = new JFileChooser();

if (evt.getSource() == open) {
int returnVal = fc.showOpenDialog(GUI.this);

if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
try {
model.incarcareContacte();
} catch (ClassNotFoundException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

}

编辑:

加载文件后,incarcareContacte 方法会在控制台上为每个条目显示一行“已恢复的联系人:cartedetelefon.Abonat@17207144”

最佳答案

您永远不会将条目重新添加到模型中,请尝试下面的代码。您可能还需要重新验证 JTable 以使其呈现新行。可能调用 jtable.fireTableDataChanged() 或类似的。

//load contacts
public void incarcareContacte() throws IOException, ClassNotFoundException {

try (
InputStream file = new FileInputStream("contacts.txt");
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream(buffer);) {
//deserialize the List
List<Abonat> recoveredContacts = (ArrayList<Abonat>) input.readObject();
//display its data
for (Abonat contacts : recoveredContacts) {
System.out.println("Recovered contacts: " + contacts);
}
listaContacte = recoveredContacts;

} catch (ClassNotFoundException ex) {
fLogger.log(Level.SEVERE, "Cannot perform input. Class not found.", ex);
} catch (IOException ex) {
fLogger.log(Level.SEVERE, "Cannot perform input.", ex);
}
}

关于java - 数据未加载回 Jtable(使用可序列化对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24562029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com