Set attribute listener
Now let's set up the listener bean, used to receive attribute changes events in MyButton and make possible responses.
Implement the PropertyChangeListener interface. There is a method called PropertyChange in this interface. Public Abstract Void PropertyChange (PropertyChangeEvent EVT) Sending the Bean calling the PropertyChange method for the property change event to notify the property change listener. In the implementation of the PropertyChange method for listening to the bean, the action to take once will be taken once the attribute changes. This makes contact between the source bean (i.e., the BEAN) of the source bean (ie, the Send Property Change Event) and the response to the response change attribute in the beanbox or other constructor. You can also handle this contact by writing a special adaptation class. For example, the listening bean named MyChangeReport contains a method ReportChange, which is the PropertyChangeEvent object. The method analyzes the new value of the attribute name and attribute from the passed object, format its text window, and display new attribute value information. Public Void ReportChange (PropertyChangeEvent EVT) {
String text = evt.getpropertyName ()
": =" evt.getnewvalue ();
Int width = getSize (). Width - 10;
Font f = getFont ();
IF (f! = null) {
// Trim The text to fit.
FontMetrics FM = GetFontMetrics (f);
While (fm.stringwidth (text)> width {
TEXT = text.substring
0, Text.Length () - 1);
}
}
Settext (text);
} You can establish contacts in the source bean and monitoring the BEAN, to do this, you need to write an adaptation class to capture the attribute change event, set an adaptation class to call the correct one in the listener object. method. Set monitoring beans, call the listener registration method on the source bean. For example, our listener calls the following methods in MyButton: MyButton.AddpropertyChangelistener (ApropertyChangelistener);