javapropertychangelistener

Why does PeropertyChangeListener in Java not work following oracle's tutorials?


Why is this not working? I have implemented this by looking at the examples at https://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html#bound and https://docs.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html Even this post did not help me Java PropertyChangeListener and others for that sake, not going to link all the posts I have looked through so what is it that I am doing wrong or have I misunderstood something about the Listener works? Here is my code:

public class Person {
    private String name;
    private String address;
    private Integer age;

    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

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

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

    public void setName(String name) {
        this.name = name;
        this.propertyChangeSupport.firePropertyChange("name", this.name, name);
    }

    public void setAddress(String address) {
        this.address = address;
        this.propertyChangeSupport.firePropertyChange("address", this.address, address);
    }

    public void setAge(Integer age) {
        this.age = age;
        this.propertyChangeSupport.firePropertyChange("age", this.age, age);
    }

}

My Listener class:

public class PersonChangeListener implements PropertyChangeListener {

    public PersonChangeListener() {
        Person person = new Person();
        person.addPropertyChangeListener(this);
    }
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        System.out.println("test");
        String value = "old value: " + evt.getOldValue() + " New value: " + evt.getNewValue();
        System.out.println(value);
    }
}

And my main entry:

public class ListenerTestMain {

    public static void main(String... args) {
        Person person1 = new Person();
        person1.addPropertyChangeListener(new PersonChangeListener());
        person1.setName("Frank");
        person1.setAddress("Street 3");
        person1.setAge(26);

        Person person2 = new Person();
        person2.setName("Joe");
        person2.setAddress("Avenue Lane 3");
        person2.setAge(26);



        if (!person1.getName().equalsIgnoreCase(person2.getName())) {
            person1.setName(person2.getName());
        }

        if (!person1.getAddress().equalsIgnoreCase(person2.getAddress())) {
            person1.setAddress(person2.getAddress());
        }

        if (!person1.getAge().equals(person2.getAge())) {
            person1.setAge(person2.getAge());
        }

    }

}

So when I am setting the new values for person1 in this case name and addressI expect the propertyChanged method to be called in PersonChangeListener for each of them. But that does not happen, at least I am not getting anything outputted in my console.

Anyway is it because the PropertyChangeListener is only working for some specific java library like javafx or the likes? Do I have to implement a custom event trigger?


Solution

  • You set the property then call the handler. At that point this.name (i.e. the oldValue) and name (i.e. the newValue) are equal, ergo the event won't fire.

    Either call the handler before setting the value (I do not advise) or use another variable to hold the old value.