Found the following example online
Listenable class:

class ListenToMe { private String variable = "Initial"; private PropertyChangeSupport support = new PropertyChangeSupport(this); public void addListener(PropertyChangeListener listener) { support.addPropertyChangeListener(listener); } public void removeListener(PropertyChangeListener listener) { support.removePropertyChangeListener(listener); } public void setVariable(String newValue) { String oldValue = variable; variable = newValue; support.firePropertyChange("variable", oldValue, newValue); } } 


Listener:

 class MyListener implements PropertyChangeListener { @Override public void propertyChange(PropertyChangeEvent event) { System.out.println("Property \"" + event.getPropertyName() + "\" has new value: " + event.getNewValue()); } } 


the listener has specified its variable

 public void setVariable(SensorState newValue) { SensorState oldValue = sensorStateLock; sensorStateLock = newValue; support.firePropertyChange("sensorStateLock", oldValue, newValue); } 

But the event of changing the value of a variable does not work.

    1 answer 1

    Worth the pattern Observer (observer) link to wikipedia

    • one
      Yes thank you! figured out. I did not immediately understand that the "observed" variable should be changed through set() - x555xx