< Day Day Up > |
How to Write a Property-Change ListenerProperty-change events occur whenever the value of a bound property changes for a bean ”a component that conforms to the JavaBeans TM specification. [32] All Swing components are also beans.
A JavaBeans property is accessed through its get and set methods . JComponent , for example, has the property font which is accessible through the getFont and setFont methods. A bound property fulfills the special requirement that, besides the get and set methods, it fires a property-change event when its value changes. Some scenarios that commonly require property-change listeners include:
Although these are some of the more common uses for property-change listeners, you can register a property-change listener on the bound property of any component that conforms to the JavaBeans specification. You can register a property-change listener in two ways. The first uses the addPropertyChangeListener(PropertyChangeListener) method. When you register a listener this way, you are notified of every change to every bound property for that object. In the propertyChange method, you can get the name of the property that has changed using the PropertyChangeEvent getPropertyName method, as in the following code snippet: KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); focusManager.addPropertyChangeListener(new FocusManagerListener()); ... public FocusManagerListener() implements PropertyChangeListener { public void propertyChange(PropertyChangeEvent e) { String propertyName = e.getPropertyName(); if ("focusOwner".equals(propertyName) { ... } else if ("focusedWindow".equals(propertyName) { ... } } ... } The second way to register a property-change listener uses the method addPropertyChangeListener(String, PropertyChangeListener) . The String argument is the name of a property. Using this method means that you only receive notification when a change occurs to that particular property. So, if you registered a property-change listener like this: aComponent.addPropertyChangeListener("font", new FontListener()); FontListener only receives notification when the value of the component's font property changes. It does not receive notification when the value changes for transferHandler, opaque , border , or any other property. The following example shows how to register a property-change listener on the value property of a formatted text field using the two-argument version of addPropertyChange- Listener : //...where initialization occurs: double amount; JFormattedTextField amountField; ... amountField.addPropertyChangeListener("value", new FormattedTextFieldListener()); ... class FormattedTextFieldListener implements PropertyChangeListener { public void propertyChanged(PropertyChangeEvent e) { Object source = e.getSource(); if (source == amountField) { amount = ((Number)amountField.getValue()).doubleValue(); ... } ...//re-compute payment and update field... } } The Property-Change Listener APITable 30 lists the methods for registering a PropertyChangeListener and Table 31 lists the methods in the PropertyChangeListener interface. Table 32 describes the methods in the PropertyChangeEvent class. Note that because PropertyChangeListener has only one method, it has no corresponding adapter class. Also refer to the PropertyChangeListener API documentation at: http://java.sun.com/j2se/1.4.2/docs/api/java/beans/PropertyChangeListener.html. The PropertyChangeEvent API documentation is at: http://java.sun.com/j2se/1.4.2/docs/api/java/beans/PropertyChangeEvent.html. Table 30. Registering a PropertyChangeListener
Table 31. The PropertyChangeListener Interface
Table 32. The PropertyChangeEvent Class
Examples That Use Property-Change ListenersThe following examples use property-change listeners.
|
< Day Day Up > |