Developing Java Beans // change the stock at

Developing Java Beans // change the stock at the specified indexstocks.setElementAt(stock, index); } } 4.3 Bound Properties In the previous example, property changes can take place when one of the setStocks() methods are invoked. This results in the state of the WatchList being changed. What would happen if two objects were using the WatchList and one of them changed the Stocks property? It’s possible that users of the WatchList will want to know if its properties are changed. A Bean component can provide change notifications for its properties. These notifications take the form of events, and they conform to the event model described in the earlier chapters. Properties that support change notifications are known as bound properties, because other objects can bind themselves to changes in their value. 4.3.1 Non-Specific Property Binding An object can bind itself to non-specific property changes. In this case the source object sends notifications to the bound object whenever the value of one of its bound properties has changed. The notification takes the form of a java.beans.PropertyChangeEvent. The public methods for this class are shown below, without their implementations: public class java.beans.PropertyChangeEvent extends java.util.EventObject{ // contructor PropertyChangeEvent(Object source, // the source of the event String propertyName, // the property name Object oldValue, Object newValue); // the old value// the new value // get the new value of the propertypublic Object getNewValue(); // get the old value of the propertypublic Object getOldValue(); // get the event propagation idpublic Object getPropagationId(); // get the name of the property that changedpublic String getPropertyName(); // set the event propagation id public void setPropagationId(); } The java.beans.PropertyChangeEvent class treats old and new values as instances of class Object. If the property that changed uses one of the primitive data types like int or float, you’ll have to use the object version of the type here, such as java.lang.Integer or java.lang.Float. It is possible that when the event is fired the old or new values are not known, or that multiple values have changed. In this case null can be returned from the getOldValue() or getNewValue() method. This is also true of the property name. If multiple properties have page 55

Comments are closed.