Developing Java Beans changed, the getPropertyName() method may
Developing Java Beans changed, the getPropertyName() method may return null. The PropertyChangeEvent also contains something called a propagation ID. This is reserved for future use. If you receive a PropertyChangeEvent and then in turn generate and fire another PropertyChangeEvent, you must propagate the propagation ID as well. A PropertyChangeEvent for a bound property should only be fired after its internal state has been updated. This means that the event signifies a change that has already taken place. If an object supports bound properties, it will provide the following methods for registering and unregistering the associated event listeners: public void addPropertyChangeListener(PropertyChangeListener p); public void removePropertyChangeListener(PropertyChangelistener p); The java.beans.PropertyChangeListener interface extends the base Java class java.util.EventListener. This interface supports a single method called propertyChange(), which takes only one parameter as an argument, of type java.beans.PropertyChangeEvent. If an object wants to receive notifications of bound property changes, it would implement the java.beans.PropertyChangeListener interface and register itself with the source object by calling its addPropertyChangeListener() method. A diagram of this interaction is shown in Figure 4.1. Figure 4.1. Notifying a listener that a property has changed Let’s go back to our Temperature class. Earlier we added a read-only property to this class named CurrentTemperature. If we wanted to implement this as a bound property, we would implement the addPropertyChangeListener() and removePropertyChangeListener() methods. Then whenever CurrentTemperature changed, we would fire a PropertyChangeEvent to all listeners. Originally we designed the Temperature class to fire a TempChangedEvent to listeners that implement the TempChangeListener interface. For now we’ll remove the code related to the firing of TempChangeEvent events. Now that we understand properties, let’s modify the Temperatureclass to fire the PropertyChangeEvent instead. Because the object does not keep track of the previous temperature, the oldValue parameter of the PropertyChangeEvent constructor is set to null. The code would now look like this: package BeansBook.Simulator; import java.beans.*; import java.util.Vector; public class Temperature page 56
Note: If you are looking for inexpensive but high quality provider to host and run your jsp application check Astra jsp hosting services