开发者

Swing beans binding: how do I notify the target that the model has changed?

开发者 https://www.devze.com 2023-01-24 10:20 出处:网络
I have a swing app with a text box bound to a property on my model 开发者_如何转开发(this is a READ_WRITE AutoBinding). The model also has an isDirty property that I want to bind to a button\'s enable

I have a swing app with a text box bound to a property on my model 开发者_如何转开发(this is a READ_WRITE AutoBinding). The model also has an isDirty property that I want to bind to a button's enabled property.

How do I properly notify the binding when I change the state of isDirty.

Here is my binding code:

BeanProperty<PaChannelConfig, Boolean> paChannelConfigBeanProperty_1 = 
                      BeanProperty.create("dirty");
BeanProperty<JButton, Boolean> jButtonBeanProperty = 
                      BeanProperty.create("enabled");
AutoBinding<PaChannelConfig, Boolean, JButton, Boolean> autoBinding_2 = 
                      Bindings.createAutoBinding(
                                       UpdateStrategy.READ, 
                                       model, 
                                       paChannelConfigBeanProperty_1, 
                                       btnApply, jButtonBeanProperty);
autoBinding_2.bind();

What is the proper way to add this notification?


Basically, the model should have the methods

addPropertyChangeListener(PropertyChangeListener)
removePropertyChangeListener(PropertyChangeListener)
firePropertyChange(PropertyChangeEvent)

look at the class PropertyChangeSupport, that class have implementations of the methods above.

In the model, the method setDirty(boolean) should be implemented like this:

public void setDirty(boolean dirty) {
    boolean old = this.dirty;
    this.dirty = dirty;
    firePropertyChange(new PropertyChangeEvent("dirty", old, dirty));
}

hope that helps


You can use PropertyChangeSupport to easily implement support for property change notification. The documentation at the provided link has an example of how to set it up and use it.

0

精彩评论

暂无评论...
验证码 换一张
取 消