0

I'm working on a big school project and it's coming to an end. I want 2 JComboBoxes to be synchronized at the different classes(JPanels) in my program, and after not knowing much about how JComponents works I found out that it's not the JComboBox itself that holds the data, but the ComboBoxModel.

So after adding a DefaultComboBoxModel to my Register (the class that holds all my data), and sharing it to my other two JPanels, it's finally working.

But now I get the NotSerializable when writing my Register-class to file. (I've been taking DefaultComboBoxModel out of my Register class and got everything back to working again, so I know that's the problem).

What I can see in the documentation, DefaultComboBoxModel implements Serialiazble, and so does my objects that the Model holds.

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;
    }

My guess is that it's not possible to use the DefaultComboBoxModel as a list to store objects in and write to file, but I'm not really sure how to solve it and still keep my program dynamic.

I hope I made my problem clear. Thanks.

EDIT2: The KlippPris class that the model holds. (ISNT THE PROBLEM)

public class KlippPris implements Serializable
{
    private int antall;
    private double rabatt;

    ...
}

EDIT3: Found out that the problem is NOT in Register, but in Salg and Administrasjon. Can't really figure out why tho...

Salg.java (Administrasjon using kinda the same lines.)

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();
    }

Why is this happening? I'm not writing neither Salg or Administrasjon to file, just the Register. I've also tried setModel() without no luck.

Not sure if the exception means anything:

com.apple.laf.AquaComboBoxUI

Java v.6, Mac OSX 10.8.3.

Amiga
  • 663
  • 8
  • 13

1 Answers1

1

In order for an object to be serialized, it must not only implement Serializable but all class members should be serializable also. Any fields that do not need to be serialized can be declared as transient:

private transient MyUnwrittenType type;
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Ok, now my program is runnable again, tho all my data in the model is lost and I get NullPointerException from the other classes calling for the model. So is my only solution to store the models object in a separate list and add them at start? Thanks. – Amiga Apr 29 '13 at 16:33
  • Hard to tell without an [sccee](http://sscce.org/). Are all fields serializanbe, for instance `KlippPris`? – Reimeus Apr 29 '13 at 16:37
  • Yes. All objects that Register holds are serializable, including the KlippPris that the DefaultComboBoxModel holds and the objects all my LinkedLists holds. I'm not really sure what more code to post tho, but I'll give it a try. As I wrote in the text, problem only occurs when I introduce the DefaultComboBoxModel in Register. – Amiga Apr 29 '13 at 16:47
  • `DefaultComboBoxModel` is serializabe including all its class member vairables. Check out the example given in [this old bug report](http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=a3a6980ac86604037bade1713d6f?bug_id=4223355). – Reimeus Apr 29 '13 at 16:55
  • I also tried to replace the `Object[] antTurer = { new KlippPris(5,0)...` with `Object[] antTurer = { "String"...` without success. – Amiga Apr 29 '13 at 16:57
  • Can't replicate this, highly likely that `KlippPris` or one of its class member variables is not serializable... – Reimeus Apr 29 '13 at 17:04
  • Well, all other class members except the `DefaultComboBoxModel` will write to file. Even if I'm just adding `String` to the model. Posted my code on `KlippPris`. Thanks for taking your time. Using Java version 6. – Amiga Apr 29 '13 at 17:17
  • What version of Java are you using? In very old versions `DefaultComboBoxModel` was not serializable. – Reimeus Apr 29 '13 at 17:19
  • Tested with 7 and was fine. I would imagine that 6 is ok too. – Reimeus Apr 29 '13 at 17:23
  • for simple JComboBox is there DefaultXxxXx, (no idea if true) for heavy changes on runtime is there, have to try with MutableComboBoxModel – mKorbel Apr 29 '13 at 19:47
  • @Reimeus found out the problem wasn't in `Register`. Sorry for misleading you! Updated all code above. – Amiga Apr 30 '13 at 09:03