Page 1 of 1

Bound Properties in Java. Rate Topic: -----

#1 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Posted 31 December 2012 - 02:13 AM

Not to be confused with the Properties class.

A Bound property allows you to notify interested listeners when the value of a field changes. It is a simple concept that once learned will instantly demonstrate its usefulness in your applications.

Imagine you have an online bank account and want to be notified when money is added to your account balance. With Bound properties, all you need do is register yourself as a listener to your balance, and you will be notified of all changes.

All the magic happens via the use of the PropertyChangeSupport class, and the PropertyChangeListener interface.

You have two simple classes, one representing your account and another representing you, the client.

Account.java
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class Account {

    /*PropertyChangeSupport manages a list of all listeners.  It notifies all registered listeners of property changes*/
    private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
    
    //your account balance.
    private int balance;

    //register a lister for updates.
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        pcs.addPropertyChangeListener(listener);
    }
    
    //remove a listener from the list of listeners.
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        pcs.removePropertyChangeListener(listener);
    }
    
    //increase account balance by i.
    public void incBalance(int i) {
        if (i > 0) {
            int oldValue = balance;
            balance += i;
            int newValue = balance;

            //The PropertyChangeSupport class moves through every one of its PropertyChangeListeners, creates a PropertyChangedEvent with these values, then calls propertyChange() on each listener.
            pcs.firePropertyChange("Balance", oldValue, newValue);
        }
        else {
            System.out.println("The amount must be greater than zero.");
        }
    }
    
    public static void main(String[] args) {
        Account acct = new Account();
        
        Client client = new Client();
        
        //add the client as a registered listener.
        acct.addPropertyChangeListener(client);
        
        //add some money and notify any listeners of the change.
        acct.incBalance(10);
        acct.incBalance(30);
        acct.incBalance(11);
        acct.incBalance(1000);
    }
}



Client.java
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 *
 * You become a listener by implementing PropertyChangeListener
 */
public class Client implements PropertyChangeListener {

    @Override
    //called by PropertyChangeSupport firePropertyChange() in Account Class. 
    public void propertyChange(PropertyChangeEvent evt) {
        //We grab the name of the property, its old value, and its new valeu then display them.
        System.out.println("Some money has been added to your account.");
        System.out.println("Previous balance: " + evt.getOldValue());
        System.out.println("New Balance: " + evt.getNewValue());
        System.out.println();
    }
}



Hopefully, that mysterious PropertyChangeListener thing has been demystified for you.

Is This A Good Question/Topic? 3
  • +

Replies To: Bound Properties in Java.

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2068
  • View blog
  • Posts: 4,301
  • Joined: 11-December 07

Posted 01 January 2013 - 11:25 AM

Thanks for the tutorial. I hadn't noticed the PropertyChangeSupport class before.
Was This Post Helpful? 1
  • +
  • -

#3 javaprịnce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-June 13

Posted 14 June 2013 - 01:41 AM

Java had always been my best language to learn. I love this explained class. But is it applicable to be used with every other objects or is there a restriction?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1