How does JavaBeans Property Adapter work?

后端 未结 1 755
庸人自扰
庸人自扰 2021-01-14 11:29

What I\'m trying to do works fine if I follow the JavaFX property definition described here. Now, instead, I want to define properties from Java Beans objects using Java Bea

1条回答
  •  抹茶落季
    2021-01-14 12:14

    I think the issue here is that Person is indeed a POJO, but not a JavaBean: It is missing the hooks for PropertyChangeListeners. Java will not magically know when Person#name changes. Instead, the JavaFX adapter will look for a way to add a PropertyChangeListener and listen to events for a property called 'name'. If you add a PropertyChangeSupport instance to your Person class it will work as expected:

    public class Person {
        private String name;
        private PropertyChangeSupport _changeSupport;
    
        public Person() {
            _changeSupport = new PropertyChangeSupport(this);
        }
    
        public String getName() {
            return name;
        }
    
        public void setName( String name ) {
            final String prev = this.name;
            this.name = name;
            _changeSupport.firePropertyChange("name", prev, name);
        }
    
        public void addPropertyChangeListener(final PropertyChangeListener listener) {
            _changeSupport.addPropertyChangeListener(listener);
        }
    }
    

    0 讨论(0)
提交回复
热议问题