beans

Bean property change event listener

In this example we shall show you how to change a Bean’s property using an event listener. We will use the PropertyChangeListener interface. This interface can be registered with a bean so as to be notified of any bound property updates. We are also using the PropertyChangeSupport class. This is a utility class that can be used by beans that support bound properties. It manages a list of listeners and dispatches PropertyChangeEvents to them. You can use an instance of this class as a member field of your bean and delegate these types of work to it. The PropertyChangeListener can be registered for all properties or for a property specified by name. To change a Bean’s property using PropertyChangeListener one should perform the following steps:

  • Create a simple Bean. The class Bean of the example has two String properties and a PropertyChangeSupport property. It has getter and setter methods for its String properties. In the setter method, the firePropertyChange(String propertyName, Object oldValue, Object newValue) API method of PropertyChangeSupport is used to report a bound property update to listeners that have been registered to track updates of every property with the specified name. In the addPropertyChangeListener(PropertyChangeListener listener) method a PropertyChangeListener is added to the listener list. The listener is registered for all properties. The same listener object may be added more than once, and will be called as many times as it is added.
  • Create a listener, like MyPropertyChangeListener in the example that implements PropertyChangeListener. Its propertyChange(PropertyChangeEvent evt) method, inherited from PropertyChangeListenermethod gets called when a bound property is changed. There we can get the property that is changed, its old value and its new value, using getPropertyName(), getOldValue() and getNewValue() API methods of PropertyChangeEvent respectively.
  • In order to run the application, we create a new instance of Bean class. We add the MyPropertyChangeListener to the new instance. Then we set values to the properties of the bean, so that the listener is triggered,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class BeanPropertyChangeEventListener {

	public static void main(String[] args) throws Exception {
		
		Bean bean = new Bean();
		bean.addPropertyChangeListener(new MyPropertyChangeListener());
		
		bean.setProperty1("newProperty1");
		bean.setProperty2(123);
		

		bean.setProperty1("newnewProperty1");
		bean.setProperty2(234);
		
	}
	
	public static class MyPropertyChangeListener implements PropertyChangeListener {
	    // This method is called every time the property value is changed
	    public void propertyChange(PropertyChangeEvent evt) {
	    	System.out.println("Name = " + evt.getPropertyName());
	
  System.out.println("Old Value = " + evt.getOldValue());
	
  System.out.println("New Value = " + evt.getNewValue());
	
  System.out.println("**********************************");
	    }
	}
	
	public static class Bean {
		
		private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
		
		// Property property1
		private String property1;
	    // Property property2
		private int property2;
	    
		public String getProperty1() {
			return property1;
		}
		public void setProperty1(String property1) {
			pcs.firePropertyChange("property1", this.property1, property1);
			this.property1 = property1;
		}
		
		public int getProperty2() {
			return property2;
		}
		public void setProperty2(int property2) {
			pcs.firePropertyChange("property2", this.property2, property2);
			this.property2 = property2;
		}
		
		public void addPropertyChangeListener(PropertyChangeListener listener) {
			pcs.addPropertyChangeListener(listener);
		}
	    
	}

}

Output:

Name
= property1
Old Value = null
New Value = newProperty1
**********************************
Name
= property2
Old Value = 0
New Value = 123
**********************************
Name
= property1
Old Value = newProperty1
New Value = newnewProperty1
**********************************
Name
= property2
Old Value = 123
New Value = 234
**********************************

 
This was an example of how to change a Bean’s property using PropertyChangeListener in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button