Java programming course: 20.3 JavaBeans


JavaBeans

A JavaBean is a reusable component that is written to conform to certain conventions. Although they don't have to be, JavaBeans are often graphical in nature and in fact all the Swing graphical components are JavaBeans. This enables them to be used within builder tools such as NetBeans.

The essential conventions that mean that a class can be considered a JavaBean are:

  • The class implements the Serializable interface
  • The class has a public constructor that takes no arguments. You can still optionally define other constructors which do have arguments, however
  • The instance variables which make up the properties of the bean are accessed using public get, set and is[1] as prefixes for the method names.
[1] For boolean properties.

Here is a class called Address which you can create in the Utilities project. To keep things simple, it only has two properties, street and city:

package com.example.util;

import java.io.*;

public class Address implements Serializable {
    
    private String street;
    private String city;
    
    public Address() {
        super();
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
    
}
 

Although not essential, it is useful for a JavaBean to fire a property change event whenever a property value has changed (i.e., each time one of the setter methods is called). The PropertyChangeSupport class (which is in the java.beans package) provides a convenient way to do this:

package com.example.util;

import java.beans.*;
import java.io.*;

public class Address implements Serializable {
    
    private PropertyChangeSupport changeSupport;
    private String street;
    private String city;
    
    public Address() {
        super();
        changeSupport = new PropertyChangeSupport(this);
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        String oldValue = this.street;
        this.street = street;
        changeSupport.firePropertyChange("street", oldValue, street);
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        String oldValue = this.city;
        this.city = city;
        changeSupport.firePropertyChange("city", oldValue, city);
    }
    
    public void addPropertyChangeListener
            (PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }
    
    public void removePropertyChangeListener
            (PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }   
}
 

The argument passed when instantiating the PropertyChangeSupport object is the object which is the source of the changes, which in this case is this object.

In each setter method the old value is saved just before updating it, and finally the firePropertyChange() method is called to notify all listeners that a change to a property has occurred.

There are three passed arguments;

  • A String which represents the property that has changed its value
  • The old value of the property before the change took place
  • The new value of the property after the change tool place

Note the two new methods to add and remove PropertyChangeListener objects, which just forward to the same methods within the PropertyChangeSupport object.

Client objects interested in being notified about property changes can implement the PropertyChangeListener interface and provide code for its propertyChange() method. Within that, you can call the PropertyChangeEvent object's getPropertyName(), getOldValue() and getNewValue() and process them as required.



Print
×
Stay Informed

When you subscribe, we will send you an e-mail whenever there are new updates on the site.

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Thursday, 11 September 2025

Captcha Image