posted 21 years ago
In order for you to be able to serialize an object, the class must implement Serializable (or Externalizable), and all instance fields of the class must either be primitives, be reference types which are also Serializable, or they must be labeled transient (not to be serialized). This rule applies to each of the Serializable objects referenced by a non-transient field. So, if your object has a field referencing another Serializable object which references another Serializable object which references another object which is not Serializable, and none of those fields were transient, then the serialization will fail, as you've seen. The problem you have is that somewhere in your chain of references, you included something that refers to a TexturePaint object, and TexturePaint is not serializable, and since it's a standard library class, you can't edit the code to make it serializable.
So, what to do? Well, to be honest, reconsider whether you really need to serialize your GUI components at all. They're probably going to contain a lot of references to things that you can't serialize. Also they're pretty bulky and inefficient as far as memory usage, and will take a while to serialize properly anyway if you do manage to avoid all the non-serializable stuff. (Though speed and memory may well be unimportant.) So see if you can avoid serializing the GUI at all. Instead, figure out what data you need to save in order to reconstruct the GUI later, and save that. E.g. you could create a GuiData class that has references to whatever data the GUI is displaying. Or a GuiConfig class with info like the user's last preferred size and position for each component, or whatever else you're trying to preserve.
It's possible there will be just one or two parts of the GUI that give you trouble (by being non-Serializable), and you'll be able to serialize most of the GUI with only a little bit of rewriting - but don't count on it. If you want to try anyway and hope for the best, start by locating where the TexturePaint is referenced by a class which you control (where you can edit the source code, that is) and insert a "transient" modifier in front of that field, so it won't be serialized. Try running the program to see if it works at all. Chanes are, the TexturePaint will now be null after deserialization, and you need to figure out how to reconstitute it from other data which has been serialized. Let's see - from the API, it looks like to create a TexturePaint, you need a BufferedImage and a Rectangle2D. Neither of those was serializable either, so you probably need to recreate those too... ugh. It's probable this is going to be too complex to bother with unless you really need to do it. In any event I doubt it's possible to give good general advice here without having access to the code; you're probably on your own. If you realy need to serialize these GUI objects, you'll need to understand how readObject() and writeObject() are used. - consult the API for ObjectOutput and ObjectInput carefully, and maybe find a book with a good section on serialization. Good luck.
"I'm not back." - Bill Harding, Twister