Question

i need to handle a change-interaction between 2 classes.

public class HeadClass {

private Subclass sub;

public void refresh() {...}

}


public class Subclass{

ArrayList store;

public void add(T data)
store.add(data);
firePropertyChange(...);
}

Whenever the method "add" in the subclass is called the method "refresh" in HeadClass should be called! But in which class i should implement this line:

private PropertyChangeSupport changes = new PropertyChangeSupport(/*WHAT SHOULD BE HERE?*/);

If i implement it in the HeadClass i can react like this:

changes.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent event) {
                refresh();
            }
        });

But how should i fire propertyChangeEvents from the Subclass if i cant access the propertyChangeSupport "changes"??

Was it helpful?

Solution

SubClass gets PropertyChangeSupport, and methods to add and remove the PropertyChangeListener's. You would pass this into SubClass's PropertyChangeSupport constructor.

public class Subclass{
  private PropertyChangeSupport propChangeSupport = 
       new PropertyChangeSupport(this);
  private ArrayList store;

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

  public void removePropertyChangeListener(PropertyChangeListener listener) {
    propChangeSupport.removePropertyChangeListener(listener);
  }


  public void add(T data)
    store.add(data);
    propChangeSupport.firePropertyChange(...);
  }


}

The key is always what class needs to have its state listened to? Since that is the SubClass, then it needs to hold the property change support. The class that does the listening is the one that adds the listener to the support.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top