- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在设置特定外观后序列化扩展 AbstractTableModel
的类会导致 java.io.NotSerializedException: com.sun.java.swing.plaf.windows.XPStyle $Skin
异常,后跟不同的 NullPointerException
。
我找到了一个解决方案,并正在回答我自己的问题,以帮助其他人节省时间,并从一开始就正确实现序列化。附件是一个在我的计算机(Win 10、Netbeans IDE 8.2、Java JDK 1.8)上重现错误的最小示例。下面的答案是主要代码段以及更多详细信息。
最佳答案
主类扩展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);
}
});
}
类(class)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)
}
主类对按下按钮使用react,成员变量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);
}
如主类注释中所示,保存并加载tip
当未设置外观或选择“金属”时,工作正常。设置“Nimbus”LaF,序列化失败并显示 NullPointerException
。设置“Windows”LaF,序列化失败并显示 java.io.NotSerializableException: com.sun.java.swing.plaf.windows.XPStyle$Skin
异常(exception),至少在我的机器上。
javax.swing.UIManager.LookAndFeelInfo
的 Javadoc声称它实现了 Serializable
,所以我没想到会出现这种异常。
使用文本编辑器检查保存的文件发现,除了 tip
的成员变量之外,还存储许多其他字段,例如 javax.swing.event.TableModelListeners
, autoCreateColumnsFromModel
还有很多。起初我怀疑这会导致异常,我只需要覆盖 writeObject(java.io.ObjectOutputStream out)
和writeObject(java.io.ObjectOutputStream out)
上课Tip
,但附加字段仍保存到磁盘。再想一想,原因就很清楚了。有趣的是,我不会猜到会保存其他字段,因为我在 AbstractTableModel
的 javadoc 中找不到这些字段的提示。也不在 TableModel
,但好吧,没那么重要。
所以有帮助的是实现一个单独的类 TipTableModel extends AbstractTableMode
接收 tip
实例化时:
public TipTableModel(Tip tip) {
this.tip = tip;
}
因此,不要序列化实现 AbstractTableModel
的类型的变量,似乎建议将数据与表模型分离并仅序列化数据。
在调试时,我还意识到扩展 AbstractListModel
的对象(列表,而不是表)除了在类中声明的成员变量之外还保存许多字段,但是序列化这些成员不会导致任何异常,尽管将数据和列表模型分开可能也是明智的。
简历:1)始终在两个单独的类中实现数据和表模型。 2)也许某些外观和感觉需要增强(尽管我仍然相信我犯了一个错误,而不是Java的开发人员),并且3)向AbstractTableModel
的javadoc添加注释澄清许多不明显的字段也被序列化。
关于java - 设置外观时序列化 AbstractTableModel 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45547229/
您可以将自定义皮肤/外观应用于 SSMS 吗?我正在考虑类似于大多数 IDE 中的深色主题(黑色背景、黄色字体)的做法 最佳答案 我决定在 SSMS 2012 上尝试一些简单的操作: 转到http:/
WordPress主题开发中如何在外观部分显示菜单选项?我可以看到主题、自定义和编辑器选项。这段代码有什么问题- function pietergoosen_theme_setup() { reg
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我正在我的一个项目中使用 JTabbedPane,并且我正在尝试更改应用程序的外观和感觉。当我设置窗口的外观和感觉(以及与此相关的任何其他外观和感觉)时,所有按钮、检查按钮等都会更改为它们应该的外观,
如何更改使用 SFAuthenticationSession 时显示的 SafariVC 上的tintColor 和 barTintColor? 我尝试更改 UINavigationBar 、 UIT
我想让 editText 看起来像这样: 设置editText的背景后我还应该做什么如何通过代码让文本在edittext中开始。 最佳答案 您是否想让 EditText 默认说“用户名”,但不是实际的
我在 didFinishLaunchingWithOptions 末尾使用以下代码 [[UITableViewHeaderFooterView Appearance] setTintColor:[UI
我的 ActionBar 中有 android.support.v7.widget.ShareActionProvider 菜单。 当我点击“分享”时,应用列表显示为弹出菜单。 当我在 Google
在使用 shiny 作为调查工具时,我想将 slider 用作“李克特式问题”。 因此,我想自定义 slider 末端的标题,使其有点像“我完全同意”和“我完全不同意”。有没有一种流畅的方法可以做到这
我正在阅读 Laravel 文档,试图更深入地了解该框架,但我在外观方面遇到了一些麻烦。我想我明白它们是什么,它们主要是为了方便访问 Laravel 核心而使用。 我对这个主题感到困惑: How do
我正在制作一款游戏,我想要一个用户可以填写表格提出想法或错误的地方,但希望它看起来与游戏图形/按钮等的其余部分相似。我不知道如何制作带图片的自定义文本字段。我希望最终结果如下所示: 我知道如何设置自定
我的 Storyboard中有多个 UINavigationController。由于我使用的是 tabbarcontroller,因此每个选项卡项都在其 ViewController 之前嵌入了自己
我需要创建一个外观,将请求转发到其他 Web 服务。 除了转发之外,外观只会对用户进行身份验证/授权。 实现这个的好方法是什么? 我曾经使用 Jersey,也许有比手动创建 WebTarget 更好的
我使用了 windows 安装程序(msi 项目),实际上我在安装后得到了 msi 文件,它会在桌面上显示一个快捷方式。 我的问题是,当我双击我的应用程序的快捷方式时,它会显示类似安装的内容,然后它会
我通过 trayIcon.displayMessage(...) 在系统托盘上显示消息,但它们看起来不太好。我试图将它们的外观更改为系统标准,但在创建图标之前调用 UIManager.setLookA
我正在尝试为我的应用程序应用深色主题,当我调用深色主题时遇到问题,代码在重新启动应用程序之前不起作用。我在设置 View Controller 中使用 UISwtich 调用黑暗模式。当我打开深色模式
我正在为我不拥有或无法访问其源代码的 Swing 应用程序编写插件。在我的部分 UI 中,我想使用特定的颜色来匹配我的图标。 在这种情况下,我想创建一个具有特定背景颜色的 JPanel。 final
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: Look and feel in java 是否有第三方漂亮的 L&F for Java swing 应用程序?默认
我想要 JButton 的外观: 我尝试通过这种方式更改 Windows 的外观: UIManager.setLookAndFeel("com.sun.java.swing.plaf.window
是否可以更改 UITabBarController 的外观? 例如,将 tabBarItems 垂直(而不是水平)排列在 View 的中心(而不是锁定在底部) 最佳答案 对于你想要的改变,没有。 您所
我是一名优秀的程序员,十分优秀!