



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CMCS 433, Fall 2001 - Alan Sussman 2
CMCS 433, Fall 2001 - Alan Sussman 3
CMCS 433, Fall 2001 - Alan Sussman 4
import java.awt.*; import java.io.Serializable; public class SimpleBean extends Canvas implements Serializable { // Constructor sets inherited properties public SimpleBean(){ setSize(60,40); setBackground(Color.red); } }
CMCS 433, Fall 2001 - Alan Sussman 5
CMCS 433, Fall 2001 - Alan Sussman 6
import java.awt.*; import java.io.Serializable; public class SimpleBean extends Canvas implements Serializable{ private Color color = Color.green; // property getter method public Color getColor(){ return color; } // property setter method. Sets new SimpleBean // color and repaints. public void setColor(Color newColor){ color = newColor; repaint(); }
CMCS 433, Fall 2001 - Alan Sussman 7
public void paint(Graphics g) { g.setColor(color); g.fillRect(20, 5, 20, 30); } // Constructor sets inherited properties public SimpleBean(){ setSize(60,40); setBackground(Color.red); } }
CMCS 433, Fall 2001 - Alan Sussman 8
CMCS 433, Fall 2001 - Alan Sussman 9
CMCS 433, Fall 2001 - Alan Sussman 10
CMCS 433, Fall 2001 - Alan Sussman 11
CMCS 433, Fall 2001 - Alan Sussman 19
// implement the PropertyChangeListener interface public class MyClass implements java.beans.PropertyChangeListener, java.io.Serializable { void propertyChange(PropertyChangeEvent evt) { // handle a property change event // e.g., call a setter method in the listener class } }
// and register the listener with the source Bean button.addPropertyChangeListener(aButtonListener); CMCS 433, Fall 2001 - Alan Sussman 20
CMCS 433, Fall 2001 - Alan Sussman 21
import java.beans.* public class Constrained … { // instantiate VetoableChangeSupport object private VetoableChangeSupport vetos = new VetoableChangeSupport(this); // methods to implement property change listener list public void addVetoableChangeListener( VetoableChangeListener l) { vetos.addVetoableChangeListener(l); } public void removeVetoableChangeListener( VetoableChangeListener l) { vetos.removeVetoableChangeListener(l); } CMCS 433, Fall 2001 - Alan Sussman 22
// modify property setter method to fire PropertyChangeEvent // including adding throws clause public void setPriceInCents(int newPriceInCents) throws PropertyVetoException { int oldPriceInCents = ourPriceInCents; // First tell the vetoers about the change. // If anyone objects, don't catch the exception // but just let it pass on to the caller. vetos.fireVetoableChange("priceInCents", new Integer(oldPriceInCents), new Integer(newPriceInCents)); // Noone vetoed, so go ahead and make the change. ourPriceInCents = newPriceInCents; changes.firePropertyChange("priceInCents", new Integer(oldPriceInCents), new Integer(newPriceInCents)); } }
CMCS 433, Fall 2001 - Alan Sussman 23
// this builds a PropertyChangeEvent object, and calls // vetoableChange(PropertyChangeEvent pce) on each registered // listener public void fireVetoableChange(String propertyName, Object oldValue, Object newValue) throws PropertyVetoException
CMCS 433, Fall 2001 - Alan Sussman 24
CMCS 433, Fall 2001 - Alan Sussman 25
CMCS 433, Fall 2001 - Alan Sussman 26
CMCS 433, Fall 2001 - Alan Sussman 27
CMCS 433, Fall 2001 - Alan Sussman 28
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { s.writeInt(ourVersion); // a static field s.writeObject(moleculeName); // a class field } private void readObject(java.io.ObjectInputStream s) throws java.lang.ClassNotFoundException, java.io.IOException { // Compensate for missing constructor reset(); if (s.readInt() != ourVersion) { throw new IOException("Molecule.readObject: version mismatch"); } moleculeName = (String) s.readObject(); }
CMCS 433, Fall 2001 - Alan Sussman 29
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { //First write out defaults s.defaultWriteObject(); //... } private void readObject(java.io.ObjectInputStream s) throws java.lang.ClassNotFoundException, java.io.IOException { //First read in defaults s.defaultReadObject(); //... }
CMCS 433, Fall 2001 - Alan Sussman 30
CMCS 433, Fall 2001 - Alan Sussman 37
public BeanDescriptor getBeanDescriptor() { return new BeanDescriptor(beanClass); } ... private final static Class beanClass = ExplicitButton.class; }
CMCS 433, Fall 2001 - Alan Sussman 38