Javabean 101 14

zhaozj2021-02-11  185

Set associated properties

The basic steps to set the associated attribute in a bean are as follows:

Turn into the Java.beans package to access some of the convenience classes defined in the package. The method of using the Import statement in MyButton is as follows: import java.beans. *; Instantiate the Java.beans.PropertyChangeSupport class. Private PropertyChangeSupport Changes = New PropertyChangeSupport (this); MyButton created a new object called Changes, which is an instance of the PropertyChangeSupport class. The variable Changes saves the collection of listeners. Once the associated property changes, these objects will be notified. . This variable defines two support methods: AddPropertyChangeListener and RemovePropertyChangeListener, which provides a public interface that allows the listener of interest to register myButton. Implement the method defined by the PropertyChangeSupport class. The PropertyChangeSupport class contains methods that add and remove the listening object, especially the PropertyChangeListener object. AddPropertyChangeListener Method Add a new listener object to the table, and the RemovePropertyChangeListener method removes a listener object from the table. The PropertyChangeSupport class also includes a third method: FirePropertyChange, which sends the PropertyChangeEvent object to the listener of interest. MyButton includes the code to add and remove the listener method as follows: Note: Parameter L represents the Property Change Listener Bean, which can be registered or removed. public void addPropertyChangeListener (PropertyChangeListener l) {changes.addPropertyChangeListener (l);} public void removePropertyChangeListener (PropertyChangeListener l) {changes.removePropertyChangeListener (l);} setter method to modify the association properties of Bean. For those properties intended to be associated properties, you can modify the setter method of the bean to include the code that sends events when the attribute value changes. MyButton calls the FirePropertyChange method within the method of setting a new attribute value. For example, when an application or user changes the font of the button, this action executes myButton.SetFont method. Because the FirePropertyChange method requires the new value and the old value of the change, the setFont method first gets the old value by calling the getFont method. After setting up the new value, then change the original value, then call the Changes.FirePropertyChange method to notify Id interest Listener. The Changes.FirePropertyChange method passes three parameters: changes in the property name, the old value of the property, the new value of this attribute.

转载请注明原文地址:https://www.9cbs.com/read-4752.html

New Post(0)