Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing components and serialization

Tags:

Why does Swing JComponent class implement the interface Serializable? The way I've implemented my views, they are stateless, all the state data is stored in a Presentation Model. So I don't need to serialize my views. I've used a @SuppressWarnings("serial") annotation to remove the warnings. Are there better ways to remove them?

like image 523
Alain Michel Avatar asked Oct 08 '09 19:10

Alain Michel


People also ask

What is the purpose of serialization in Swing?

Serialization is simply turning an existing object into a byte array. This byte array represents the class of the object, the version of the object, and the internal state of the object. This byte array can then be used between JVM's running the same code to transmit/read the object.

What are the five Java Swing components?

In Java Swing, there are a number of components like a scroll bar, button, text field, text area, checkbox, radio button, etc. All these components together, form a GUI that offers a rich set of functionalities and also allows high-level customization.

How many components are in a Swing?

The Swing package defines two types of components: top-level containers ( JFrame , JApplet , JWindow , JDialog ) lightweight components ( Jeverything-else , such as JButton , JPanel , and JMenu )


2 Answers

In the beginning GUI builders were going to save the UI in serialised format. The applet tag even has an attribute to load from serialised form (I don't know anyone else who has used that, and I've only used it to be malicious). Unfortunately using the serialisation mechanism for GUIs doesn't really work. Swing only guarantees compatibility in within the same major release (and I'm guessing even that has few tests).

like image 143
Tom Hawtin - tackline Avatar answered Oct 09 '22 13:10

Tom Hawtin - tackline


Why does Swing JComponent class implement the interface Serializable?

Although this allows you to serialize classes and send them to and from the client and the server, this does not seem to be an intuitive scenario. There is a better chance, still slim, that someone might want to serialize components to a file. This will allow for that sort of serialization.

Are there better ways to remove [the warnings]?

You could instantiate the serialVersionUID, but if you do so you will need to maintain it when you class changes. This seems like overkill. Another option, as Laurence Gonsalves points out in his comment, is to suppress the warnings altogether in the Preferences->Java->Compiler->Errors/Warnings->Potential Programming Problems field.

like image 23
akf Avatar answered Oct 09 '22 14:10

akf