Developing Java Beans The ListeningPanel class implements writeObject()
Developing Java Beans The ListeningPanel class implements writeObject() for storing its state. This method invokes defaultWriteObject() to perform default serialization on the data members of the panel. Next, it creates an instance of java.lang.String and populates it with the concatenation of the button labels. The string is then written to the stream. This string will be retrieved during reconstruction as a sanity check. This is another example of saving additional data to the object stream. The readObject() method is also implemented by ListeningPanel. The first thing it does is invoke defaultReadObject() to reconstruct the data members. Next it reads an instance of java.lang.String. This is the string that was written out previously as a sanity check on the serialization. A new sanity check string is created by concatenating the button labels, and it is compared to the string that was retrieved from the object stream. Here is the code for the ListeningPanel class: import java.awt.*; import java.awt.event.*; import java.io.*; import BeansBook.util.*; public class ListeningPanel extends Panel { // the buttonsprotected Button b1; protected Button b2; protected Button b3; // the button adapter protected GenericButtonAdapter adapter; // the constructor public ListeningPanel() { } // initialize the panel public void initialize() { // construct the buttons b1 = new Button(”Button 1″); b2 = new Button(”Button 2″); b3 = new Button(”Button 3″); // add the buttons to the panel add(b1); add(b2); add(b3); // setup the adapter try { adapter = new GenericButtonAdapter(this); adapter.registerActionEventHandler(b1, “handleB1″); adapter.registerActionEventHandler(b2, “handleB2″); adapter.registerActionEventHandler(b3, “handleB3″); } catch (Exception e) { } } // handle click for b1 public void handleB1(ActionEvent evt) page 87