OK, I haven't done much with Serialization. I have a simple program that creates four JPanels with different background colors and adds them to a larger JPanel using a 2 x 2 GridLayout. That larger JPanel is added to a JFrame. I've written my own serialization code to override the default. All I am storing is one integer for each of the four JPanels, representing the background colors of the smaller JPanels. I'm doing this because I have heard that it was a bad idea to actually serialize the JPanels themselves due to portability issues? Is that true? Anyway, it seems to work, but looking at the files that are produced, they are storing a lot of stuff, not just integers. For example:
¬í sr BPanelwv#hçµ [ panelst
[[LAPanel;xr javax.swing.JPanel]D
„™B$ xr javax.swing.JComponentñ³ã%Àe F
alignmentXF
alignmentYZ autoscrollsI flagsZ isAlignmentXSetZ isAlignmentYSetZ verifyInputWhenFocusTargetL accessibleContextt 'Ljavax/accessibility/AccessibleContext;L actionMapt Ljavax/swing/ActionMap;L ancestorInputMapt Ljavax/swing/InputMap;L bordert Ljavax/swing/border/Border;L
focusInputMapq ~ L
inputVerifiert Ljavax/swing/InputVerifier;L listenerListt %Ljavax/swing/event/EventListenerList;L popupMenut Ljavax/swing/JPopupMenu;L vetoableChangeSupportt "Ljava/beans/VetoableChangeSupport;L windowInputMapt Ljavax/swing/ComponentInputMap;xr java.awt.Container@€sý' I containerSerializedDataVersionZ focusCycleRootZ focusTraversalPolicyProviderI ncomponents[ componentt [Ljava/awt/Component;L
dispatchert Ljava/awt/LightweightDispatcher;L layoutMgrt Ljava/awt/LayoutManager;L maxSizet Ljava/awt/Dimension;xr java.awt.Component•ê¦Y×<¤š #I boundsOpI componentSerializedDataVersionZ enabledJ eventMaskZ focusTraversalKeysEnabledZ focusableI heightZ
ignoreRepaintI isFocusTraversableOverriddenZ isPackedZ
maxSizeSetZ
minSizeSetZ nameExplicitlySetZ
newEventsOnlyZ prefSizeSetZ validZ visibleI widthI xI yL accessibleContextq ~ L
backgroundt Ljava/awt/Color;L
changeSupportt "Ljava/beans/PropertyChangeSupport;L cursort Ljava/awt/Cursor;L
dropTargett Ljava/awt/dnd/DropTarget;[ focusTraversalKeyst [Ljava/util/Set;L fontt Ljava/awt/Font;L
foregroundq ~ L localet Ljava/util/Locale;L maxSizeq ~ L minSizeq ~ L namet Ljava/lang/String;L peerFontq ~ L popupst Ljava/util/Vector;L prefSizeq ~ xp psr javax.swing.plaf.ColorUIResourcekSùŸòêæ’ xr java.awt.Color¥ƒ3u F falphaI valueL cst Ljava/awt/color/ColorSpace;[ frgbvaluet [F[ fvalueq ~ xp ÿîîîpppppppsr javax.swing.plaf.FontUIResourceBćÁ"‹G xr
java.awt.FontÅ¡5æÌÞVs I fontSerializedDataVersionF pointSizeI sizeI styleL fRequestedAttributest Ljava/util/Hashtable;L nameq ~ xp A@ pt Dialogxsq ~ ÿ333pppsr java.util.Locale~ø`œ0ùì I hashcodeL countryq ~ L languageq ~ L variantq ~ xpÿÿÿÿt USt ent pppppppsr java.awt.ComponentOrientationÆê§E¡œcÌ I orientationxp ppx ur [Ljava.awt.Component;‰
æªu xp sr APaneld5™\ÒÀZ I
colorIndexxq ~ psq ~ ÿ ÿÿpppppppq ~ %q ~ 'q ~ )pppppppq ~ .ppx uq ~ / psr java.awt.FlowLayout›6K[ù9 I alignZ alignOnBaselineI hgapI newAlignI serialVersionOnStreamI vgapxp pppx @ ppppppsr #javax.swing.event.EventListenerList±6Æ}„êÖD xppxpppw xxw xsq ~ 1 psq ~ ÿ pppppppq ~ %q ~ 'q ~ )pppppppq ~ .ppx q ~ 4psq ~ 5 pppx @ ppppppsq ~ 7pxpppw xxw xsq ~ 1 pq ~ :ppppq ~ %q ~ 'q ~ )pppppppq ~ .ppx q ~ 4psq ~ 5 pppx @ ppppppsq ~ 7pxpppw xxw xsq ~ 1 pq ~ :ppppq ~ %q ~ 'q ~ )pppppppq ~ .ppx q ~ 4psq ~ 5 pppx @ ppppppsq ~ 7pxpppw xxw xpsr java.awt.GridLayout™#úëKÜë I colsI hgapI rowsI vgapxp pppx @ ppppppsq ~ 7pxpppw xxq ~ 2q ~ 9q ~ =q ~ @x
It's clearly storing a lot of the stuff that I'm not asking it to, like the fact that it's a GridLayout. I don't need or want it to store all the dimensions and stuff like that, but it is anyway. Why? And am I correct that you're not supposed to serialize JPanels due to portability? That's why I just wanted to store an integer and recreate everything from that.
Here's the code:
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class BPanel extends JPanel implements Serializable
{
APanel panels[][];
public BPanel (APanel thepanels[][])
{
panels = thepanels;
this.setLayout (new GridLayout (2, 2));
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
this.add (panels[i][j]);
}
}
}
private void readObject(ObjectInputStream ois)
{
try
{
panels = new APanel[2][2];
this.setLayout (new GridLayout (2, 2));
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
panels[i][j] = (APanel) ois.readObject();
this.add (panels[i][j]);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void writeObject(ObjectOutputStream os)
{
try
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
os.writeObject (panels[i][j]);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class APanel extends JPanel implements Serializable
{
static Color colors[] = {Color.RED, Color.BLACK, Color.MAGENTA, Color.BLUE, Color.CYAN};
int colorIndex;
public APanel (int index)
{
this.setBackground (colors[index]);
colorIndex = index;
}
private void readObject(ObjectInputStream ois)
{
try
{
colorIndex = ois.readInt();
this.setBackground(colors[colorIndex]);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void writeObject(ObjectOutputStream os)
{
try
{
os.writeInt (colorIndex);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;
import javax.swing.*;
public class Main
{
public static void main (String args[])
{
if (args.length != 2)
{
System.out.println ("Usage: 'java Main R filename' or 'java Main W filename'");
System.exit (1);
}
boolean read = false;
if (args[0].equals("R"))
read = true;
String filename = args[1];
BPanel bpanel = null;
APanel apanels[][] = new APanel[2][2];
if (!read)
{
Random random = new Random ();
int colorIndexes[][] = new int[2][2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
colorIndexes[i][j] = random.nextInt(APanel.colors.length);
apanels[i][j] = new APanel (colorIndexes[i][j]);
}
}
bpanel = new BPanel (apanels);
try
{
FileOutputStream fs = new FileOutputStream(filename);
ObjectOutputStream os = new ObjectOutputStream(fs);
/* for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
os.writeObject (apanels[i][j]);
}
}*/
os.writeObject(bpanel);
os.flush();
os.close();
}
catch (Exception e)
{
e.printStackTrace();
System.exit (1);
}
}
else
{
try
{
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(fis);
/* for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
apanels[i][j] = (APanel) ois.readObject();
}
}*/
// bpanel = new BPanel (apanels);
bpanel = (BPanel) ois.readObject ();
ois.close();
}
catch (Exception e)
{
e.printStackTrace();
System.exit (1);
}
}
JFrame frame = new JFrame ();
frame.setSize (500, 500);
frame.setVisible (true);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane ().add (bpanel);
}
}