【发布时间】:2013-04-23 09:40:44
【问题描述】:
我正在处理一个大型学校项目,它即将结束。 我希望在我的程序中的不同类(JPanels)上同步 2 个 JComboBox,在不太了解 JComponents 的工作原理之后,我发现保存数据的不是 JComboBox 本身,而是 ComboBoxModel。
因此,在向我的 Register(保存我所有数据的类)添加 DefaultComboBoxModel 并将其共享给我的其他两个 JPanel 之后,它终于可以工作了。
但是现在我在将我的 Register 类写入文件时得到了 NotSerializable。 (我一直在将 DefaultComboBoxModel 从我的 Register 类中取出,并让一切恢复正常,所以我知道这是问题所在。
我在文档中可以看到,DefaultComboBoxModel 实现了 Serialiazble,我的模型持有的对象也是如此。
public class Register implements Serializable
{
...
private DefaultComboBoxModel klippPriser;
public Register()
{
...
Object[] antTurer = { new KlippPris(5,0), new KlippPris(10,0.05), new KlippPris(15,0.10) };
klippPriser = new DefaultComboBoxModel(antTurer);
}
public DefaultComboBoxModel getKlippPriser()
{
return klippPriser;
}
我的猜测是不可能使用 DefaultComboBoxModel 作为列表来存储对象并写入文件,但我不确定如何解决它并仍然保持程序动态。
我希望我把我的问题说清楚了。谢谢。
EDIT2:
模型拥有的 KlippPris 类。 (不是问题)
public class KlippPris implements Serializable
{
private int antall;
private double rabatt;
...
}
EDIT3:
发现问题在Register 中不是,而是在Salg 和Administrasjon 中。
实在想不通为什么...
Salg.java(Administrasjon 使用了相同的行。)
public class Salg extends JPanel
{
private Register register;
...
private JComboBox antTurer;
public Salg(Register r)
{
super();
register = r;
gui();
}
private void gui()
{
...
paintKortkjøp();
...
}
private void paintKortkjøp()
{
...
// This line cause the exception.
antTurer = new JComboBox(register.getKlippPriser());
// This line dosen't cause any problems, but won't let my use my model.
antTurer = new JComboBox();
}
为什么会这样?我没有写Salg 或Administrasjon 到文件,只是注册。
我也尝试过setModel(),但没有成功。
不确定异常是否意味着什么:
com.apple.laf.AquaComboBoxUI
Java v.6,Mac OSX 10.8.3。
【问题讨论】:
标签: java notserializableexception comboboxmodel