The basic Steps for setting association properties in a bean are as follows:
The Java.beans package is transferred in order to access some of the convenience classes defined in the package. The import statement in MyButton is used as follows:
Import java.beans.*;
Instantiate the Java.beans.PropertyChangeSupport class.
Private Propertychangesupport changes = new
Propertychangesupport (this);
MyButton creates a new object named changes, which is an instance of the Propertychangesupport class, and the variable changes holds a collection of listener objects that are notified once the associated properties change. The variable defines two supported methods: Addpropertychangelistener and Removepropertychangelistener, which provide a common interface that allows interested listeners to register the MyButton.
Implement the method defined by the Propertychangesupport class.
The Propertychangesupport class includes methods for adding and removing listener objects, especially PropertyChangeListener objects. The Addpropertychangelistener method adds a new listener object to the table, and the Removepropertychangelistener method removes a listener object from the table. The Propertychangesupport class also contains a third method: Firepropertychange, which sends the Propertychangeevent object to the listener who is interested. MyButton contains the following code that implements adding and removing listener methods:
Note: Parameter L represents the property change listener bean that can be registered or removed from its interest.
Modifies the setter method of the bean's associated properties.
For properties that are intended to be associated, you can modify the bean's setter method to include code that sends the event when the value of the property changes. MyButton calls the Firepropertychange method within each method that sets a new property value. For example, when an application or user changes the font of a button, the action executes the Mybutton.setfont method. Because the Firepropertychange method requires both new and old values for the changed property, the SetFont method first obtains the old value by invoking the GetFont method, then sets the new value, and after changing the original value, Then call the Changes.firepropertychange method to notify the listener who is interested. The Changes.firepropertychange method passed three parameters: the changed property name, the old value of the property, and the new value of the property.
public void SetFont (Font f) {
Font old = GetFont ();
Super.setfont (f);
SizeToFit ();
Changes.firepropertychange (
"Font", old, F);
}
What does it accomplish for the Firepropertychange method? The method binds its three parameters to a Propertychangeevent object. The Propertychangeevent object is then used as a parameter to invoke the PropertyChange method. Pass the Propertychangeevent object to each registered listener. Remember: PropertyChange treats the old value and the new value of the attribute as an object value. This is important, if your attribute value is a simple type, you must redefine the type as an object, for example, a simple integer type that is converted to Java.lang.Integer before calling Firepropertychange.
Remember: for the associated property, the property value is first changed before the property change event is sent.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service