Serializing swing/awt components

前端 未结 4 1033
执念已碎
执念已碎 2021-01-27 18:01

I am trying to serialize a JPanel but everytime i get this error:

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: javax.swing         


        
相关标签:
4条回答
  • 2021-01-27 18:19

    JPanel implements Serializable...

    But javax.swing.GroupLayout don't.

    So, you need to change GroupLayout to another layout class!

    0 讨论(0)
  • 2021-01-27 18:32

    GroupLayout, which appears to have been added to the panel, is not serialisable. There are a few options (listed least to most favourable).

    • Subclass GroupLayout to create a serial proxy (see Effective Java 2nd Ed). This is a fair bit of work, and is complicated by GroupLayout not having a complete set of "getters and setters".

    • Replace GroupLayout with a serialisable LayoutManager. There's the functional but rough GridBagLayout in the Java library. Other layout managers are available.

    • Don't serialise the JPanel. Making AWT components serialisable (and the whole JavaBeans thing) was a laughable mistake.

    0 讨论(0)
  • 2021-01-27 18:34

    To serialize an object, all the objects it references need to also be serializable aswell. You can mark a reference transient if you don't want it to be serialized. This means, if your panel refers to objects which are not serilizable, make them serilizable aswell or mark them transient

    0 讨论(0)
  • 2021-01-27 18:43

    To be serializeable the object (and all the parts of it with exception of the POJOs) needs to implement the Serializable interface.

    If you cannot change the class, take a look at XStream.

    0 讨论(0)
提交回复
热议问题