JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

How to Write Bean PropertyChangeListener in Java

April 7, 2014 by Krishna Srinivasan Leave a Comment

Property change events occur when ever there is change in the property values. We assume that the component conforms with the Java Bean specification. JavaBean s property is accessed using the set or get methods. These methods are invoked by the application for setting or getting the values from the bean. This doesn’t have any logic for the application.

Java provides PropertyChangeListener for binding the bean’s properties to the event listener. When ever a property is updated, the event will be fired and listener method executed. This example demonstrates how to write a PropertyChangeListener and attach the listener to a bean. When ever property value is changed, the method will be invoked. Lets look at the example.

  • PropertyChangeListener interface extends the EventListener
  • This interface has only one method propertyChange(PropertyChangeEvent evt)

BeanPropertyChangeListener.java

[code lang=”java”]
package javabeat.net.core;
/**
* Bean property change listener example
*
*/
public class BeanPropertyChangeListener {
public static void main(String[] args) {
Bean bean = new Bean("Default Name","Default City");
bean.addPropertyChangeListener(new PropertyChangeListenerExample());
bean.setName("Name 1");
bean.setCity("City 1");
bean.setName("Name 2");
bean.setCity("City 2");
}

}
[/code]

Bean.java

[code lang=”java”]
package
javabeat.net.core;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class Bean {
public Bean(String name, String city) {
this.name = name;
this.city = city;
}

PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private String name;
private String city;

public String getName() {
return name;
}

public void setName(String name) {
changeSupport.firePropertyChange("name", this.name, name);
this.name = name;
}

public String getCity() {
return city;
}

public void setCity(String city) {
changeSupport.firePropertyChange("city", this.city, city);
this.city = city;
}

public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}

}
[/code]

PropertyChangeListenerExample.java

[code lang=”java”]
package
javabeat.net.core;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class PropertyChangeListenerExample implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("Property Name : " + evt.getPropertyName());
System.out.println("Old Value : " + evt.getOldValue());
System.out.println("New Value : " + evt.getNewValue());
}
}
[/code]

Output…

[code]
Property Name : name
Old Value : Default Name
New Value : Name 1
Property Name : city
Old Value : Default City
New Value : City 1
Property Name : name
Old Value : Name 1
New Value : Name 2
Property Name : city
Old Value : City 1
New Value : City 2
[/code]

Filed Under: Java Tagged With: Java Bean

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved