How do I make a JavaBeans property bound?

John Zukowski

You need to implement a pair of methods to maintain list of property change listeners:

public void addPropertyChangeListener(PropertyChangeListener x)
public void removePropertyChangeListener(PropertyChangeListener x)

The utility class PropertyChangeSupport is available to do most of the work for you, managing the list and providing notification support.

The registration support would look like:

private PropertyChangeSupport changes = 
  new PropertyChangeSupport(this);

public void addPropertyChangeListener(
              PropertyChangeListener p) {
  changes.addPropertyChangeListener(p);
}

public void removePropertyChangeListener(
              PropertyChangeListener p) {
  changes.removePropertyChangeListener(p);
}

The bound property would then look something like the following:

public void setMood(int mood) {
  int old = this.mood;
  this.mood = mood;
  repaint();
  changes.firePropertyChange("mood",
    new Integer(old), new Integer(mood));
}

And the object interested in the change would need:

public void propertyChange
   (PropertyChangeEvent e) { 
  ...
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact