aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Serializable vs. XMLEncoder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Serializable vs. XMLEncoder" Watch "Serializable vs. XMLEncoder" New topic
Author

Serializable vs. XMLEncoder

Christopher Arthur
Ranch Hand

Joined: Mar 09, 2004
Posts: 149
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

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Oct 13, 2005
Posts: 38363
    
  23
. . . and I don't know much about Beans; try this part of the Java tutorial.
Jared Cope
Ranch Hand

Joined: Aug 18, 2004
Posts: 243
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.


SCJP 1.4 91%, SCJP 1.5 88%, SCJD B&S
Christopher Arthur
Ranch Hand

Joined: Mar 09, 2004
Posts: 149
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

Joined: Mar 09, 2004
Posts: 149
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

Joined: Oct 13, 2005
Posts: 38363
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serializable vs. XMLEncoder