2.2.2 use the property change listener as the observer
In addition to the basic event delegation mechanism, JavaBean introduces a variant of the observer design pattern, this time through the property change listener. PropertyChangeListener is the exact representation of the observer mode. Each Observer observes the change of a Subject attribute. When a change occurs in a Subject, the observer is notified of a new State. Figure 2-4 shows the observer mode structure associated with a specific class in the JavaBean library for attribute change processing. In this case, the observed Subject has a set of listener methods for adding/remove attribute changes and a property whose status is monitored.
With PropertyChangeListener, the registered listener set is managed in the PropertyChangeSupport class. When the monitored property value changes, this support class notifies all registered listeners of New and Old property status values.
By registering a PropertyChangleListener object with various components that support this listener type, we can reduce the amount of code that must be generated after the listener settings are initialized. For example, if you bind the background color of the Swing component, you can register a PropertyChangeListener with the component. When the background settings change, you will be notified. When the background attribute value of the component changes, the listener can be notified so that the observer can set the background color to a new one. Therefore, if we want all components in our program to have the same background color, we can register them to a component. Then, when that component changes its background color, all other components will be notified of this change and its background will be changed to a new setting.
The programs in List 2-3 demonstrate the use of PropertyChangeListener. It creates two buttons. When any selected button is selected, the background of the selected button is changed to a random color value. The second button monitors the attribute changes of the first button. When the background color of the first button changes, the background color of the second button is changed to a new value. The first button does not listen for the attribute changes of the second button, so when the second button is selected and changed and the background color, this change will not spread to the first button.
/**
*
*/
Package swingstudy. ch02;
Import java. awt. BorderLayout;
Import java. awt. Color;
Import java. awt. EventQueue;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. beans. PropertyChangeEvent;
Import java. beans. PropertyChangeListener;
Import java. util. Random;
Import javax. swing. JButton;
Import javax. swing. JFrame;
/**
* @ Author lenovo
*
*/
Public class ButtonSample3 {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Runnable runner = new Runnable (){
Public void run (){
JFrame frame = new JFrame ("Button Sample ");
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
Final JButton button1 = new JButton ("Select Me ");
Final JButton button2 = new JButton ("No Select Me ");
Final Random random = new Random ();
ActionListener actionListener = new ActionListener (){
Public void actionreceivmed (ActionEvent event ){
JButton button = (JButton) event. getSource ();
Int red = random. nextInt (255 );
Int green = random. nextInt (255 );
Int blue = random. nextInt (255 );
Button. setBackground (new Color (red, green, blue ));
}
};
PropertyChangeListener propertyChangeListener = new PropertyChangeListener (){
Public void propertyChange (PropertyChangeEvent event ){
String property = event. getPropertyName ();
If ("background". equals (property )){
Button2.setBackground (Color) event. getNewValue ());
}
}
};
Button1.addActionListener (actionListener );
Button1.addPropertyChangeListener (propertyChangeListener );
Button2.addActionListener (actionListener );
Frame. add (button1, BorderLayout. NORTH );
Frame. add (button2, BorderLayout. SOUTH );
Frame. setSize (300,100 );
Frame. setVisible (true );
}
};
EventQueue. invokeLater (runner );
}
} Although this example only implements a color change in the selected button, imagine that the background color of the first button is changed from hundreds of different positions, rather than an action listener. If there is no property change listener, each of these positions needs to change the background color of the second button. By using the property change listener, you only need to modify the background color of the basic object-in this case, it is the first button. Then this change will be automatically transmitted to other components.
The Swing Library also uses the ChangeEvent/ChangeListener pair to indicate State changes. Although it is similar to PropertyChangeEvent/PropertyChangeListener, ChangeEvent does not have new or old data value settings. We can regard it as a lightweight version of the property change listener. ChangeEvent is useful when multiple attribute values change, because ChangeEvent does not need to be packaged.