主类扩展JFrame,设置外观并显示JTable。它有一个成员变量Tip tip,其中Tip 扩展了AbstractTableModel(见下文)。
public class MainFrame extends javax.swing.JFrame {
// a single tip
// in my final app, there was a list of tips
Tip tip = new Tip();
public MainFrame() {
initComponents();
// fill new instance with some dummy data for answers
tip.addAnswer("first answer", 1, "first reply");
tip.addAnswer("second answer", 2, "second reply");
// assign the table model
jTable.setModel(tip);
}
// ... more code (see attachment)
public static void main(String args[]) {
/* Set the look and feel */
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
/**
* when setting LaF to 'Nimbus' de-/serialization fails on the first save/load:
* Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
* at javax.swing.plaf.synth.SynthLookAndFeel.paintRegion(SynthLookAndFeel.java:371)
*
* when setting LaF to 'Windows' the second save/load fails with:
* java.io.NotSerializableException: com.sun.java.swing.plaf.windows.XPStyle$Skin
* at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
*
* when setting LaF to 'Metal' save/load works just fine
*
* not setting LaF at all (uncomment the line below) also works fine
*/
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
Tip 类包含一些应该在表格中显示的成员变量(自定义类型TipAnswer),以及业务逻辑所需但未在表格中显示的其他成员变量:
public class Tip extends AbstractTableModel {
// ... more member variables that don't show up in the table
// a list of answers to the tip which show up in the table
private ArrayList<TipAnswer> answers = new ArrayList<>();
// adds an answer to the tip
public void addAnswer(String answer, int cost, String reply) {
answers.add(new TipAnswer(answer, cost, reply));
}
// ... more methods that override the methods
// required by AbstractTableModel (see attachment)
}
主类在按下按钮时做出反应,成员变量tip被序列化并立即再次反序列化。反序列化的结果如表所示。
private void jSaveAndLoadActionPerformed(java.awt.event.ActionEvent evt) {
// make temp file
Path path;
try {
path = Files.createTempFile("TestTableModel", ".txt");
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
return;
}
// write to file (serialize)
try ( FileChannel channel = FileChannel.open(path, StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
ObjectOutputStream oos = new ObjectOutputStream(Channels.newOutputStream(channel))
) {
// write object to file
oos.writeObject(tip);
System.out.println("Tip table saved as " + path);
} catch (Exception ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
return;
}
// set an empty table model for demonstration purposes
jTable.setModel(new Tip());
// read from file (deserialize)
Tip tipFromFile;
try ( FileChannel channel = FileChannel.open(path, StandardOpenOption.READ);
ObjectInputStream ois = new ObjectInputStream(Channels.newInputStream (channel)) ) {
// the instance to return
tipFromFile = (Tip)ois.readObject();
} catch (Exception ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
return;
}
// store the tip which has just been read in this instance
tip = tipFromFile;
// show the tip in the table
jTable.setModel(tip);
}
如主类的 cmets 所示,当未设置外观或选择“金属”时,保存和加载 tip 可以正常工作。设置 'Nimbus' LaF,序列化失败并显示 NullPointerException。设置 'Windows' LaF,序列化失败并出现 java.io.NotSerializableException: com.sun.java.swing.plaf.windows.XPStyle$Skin 异常,至少在我的机器上是这样。
javax.swing.UIManager.LookAndFeelInfo 的 Javadoc 声称它实现了 Serializable,所以我没想到会出现这种异常。
使用文本编辑器检查保存的文件会发现,除了tip 的成员变量之外,还存储了许多其他字段,例如javax.swing.event.TableModelListeners、autoCreateColumnsFromModel 等等。起初我怀疑这会导致异常,我只需要覆盖Tip 类中的writeObject(java.io.ObjectOutputStream out) 和writeObject(java.io.ObjectOutputStream out),但附加字段仍然保存到磁盘。再想一想,原因就很清楚了。有趣的是,我没想到会保存其他字段,因为我在 AbstractTableModel 和 TableModel 的 javadoc 中找不到这些字段的提示,但好吧,不是那么重要。
因此,帮助实现一个单独的类TipTableModel extends AbstractTableMode,在实例化时接收tip:
public TipTableModel(Tip tip) {
this.tip = tip;
}
因此,与其序列化实现 AbstractTableModel 的类型的变量,不如将数据与表模型分离并仅序列化数据。
经过调试,我也意识到,扩展AbstractListModel(List,not Table)的对象除了类中声明的成员变量外,还保存了很多字段,但是序列化这些成员并没有导致任何异常,虽然将数据和列表模型分开可能也是可取的。
简历:1) 始终在两个单独的类中实现数据和表模型。 2) 也许某些 Look-and-Feels 需要增强(尽管我仍然认为我犯了一个错误,而不是 Java 的开发人员),以及 3) 在 AbstractTableModel 的 javadoc 中添加注释以澄清许多非显而易见的字段也被序列化了。
Netbeans project of this example.