Swing components and serialization
Why does Swing JComponent
class implem开发者_高级运维ent 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?
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).
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.
精彩评论