File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serializable vs. XMLEncoder

 
Christopher Arthur
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It occurred to me that a really lazy way to save the state of all the buttons, check boxes, text fields, etc, would be to just make them all instance variables on one big serializable class, then you can write the whole thing out to a file. But the 1.4.2 API cautions against this technique, claiming future compatibility problems. The alternative given is to use the XMLencode/decode api. The only problem with this, according to the API docs, is that you need to code according to the JavaBean guidelines for this to work. What does this mean, exactly? is it enough to have getter and setter methods for everything i want to save?

For example, if there's a JCheckBox whose state I want to recover, is it enough to have lines like this:

public class MyGUI implements Serializable {

JCheckBox myCB;
public JCheckBox getMyCB(){return myCB;}
public void setMyCB(JCheckBox myCB){this.myCB = myCB;}

}


}
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization: Depends how long you want to keep the object for. You can probably get away with it for a week or so.

And be careful about putting Serializable on everything. Many classes in Swing implement Serializable already, so it's unnecessary.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and I don't know much about Beans; try this part of the Java tutorial.
 
Jared Cope
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Originally posted by Campbell Ritchie:
Serialization: Depends how long you want to keep the object for. You can probably get away with it for a week or so.


Just curious of what you meant by this. I don't think there is a time limit of this sort of stuff. You seem to imply that you will be in trouble after a couple of weeks.

Only problem you will have is if you try to deserialize with a future version of the JRE where things may have changed in the core classes between releases. If you never update the JRE you will be fine forever.

Cheers, Jared.
 
Christopher Arthur
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a test I put everything in a hierarchy below a JPanel, then used XMLEncoder to save the state as a single file. It complained about all kinds of things in the process, but eventually produced a file. Mostly, but not completely, did the XMLDecoder remake the JPanel. My guess is that it got confused for one component that has a Java2D drawing on it.
I think that I can tweak my code to get this to work without too much effort, and actually i didn't need to do any bean programming at all.
 
Christopher Arthur
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm...well it almost works, but now i'm getting encoding errors like:



Also, it seems that by default the encoder preserves EVERYTHING about the swing components, which turns out to be far too much overhead for a simple options file.

[ June 23, 2006: Message edited by: Christopher Arthur ]
[ June 23, 2006: Message edited by: Christopher Arthur ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only problem you will have is if you try to deserialize with a future version of the JRE where things may have changed in the core classes between releases. If you never update the JRE you will be fine forever.
Jared: yes, that is what I meant. Sorry I put it so badly.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic