Serialize a class using SpringLayout (Java)

205 views Asked by At

I recently started developing in Java and have enjoyed the experience a lot. The default Java package comes loaded with useful things, and having the Swing package has been a great help creating a GUI. I decided to use SpringLayout for my LayoutManager because it was simple and extremely flexible, but now down the road I'm finding myself in a pickle.

I would like to be able to persist the state of my application. I started to approach this with use of the Preferences API, but that only allows for the storage of Java natives (understandable - its goal is to be lightweight). I went ahead and bastardized the use of Preferences by persisting XML strings packed with data. After a while, I decided to look around and see if there was anything better, and then I got around to reading about serializable - looked like I had found my answer.

I hit a snag, however, when I tried to serialize a class that had some GUI components that were managed by SpringLayout, which I have come to find does not implement serializable. I thought I'd found my way around the issue with the transient keyword, but SpringLayout's only allowable modifier is final. The only workaround I've seen is to change which LayoutManager I'm using... which I'd rather not do as I've created some fairly customized GUIs.

Any ideas?

Thanks in advance for any and all responses.

-M

2

There are 2 answers

1
MandM On

I was trying to declare instances of SpringLayout that were local to the constructor as transient. What needed to be done was to declare those variables as class variables and then modify them as transient.

3
mercutio On

What do you mean by

SpringLayout's only allowable modifier is final.

If it is a field of your Serializable Object then you can declare it transient. If you do not have control over all fields in an Object, try persisting only a State Object where you write all data that is relevant. Persisting a layout manager is not a good idea.