是什么让模型更改通过 JFace 数据绑定传播?
为了了解 JFace 数据绑定是如何工作的,我有一个具有两个属性的模型对象。更改一个属性应将另一个属性设置为相同的值:
public class Model {
private double x;
private double y;
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
public void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
public void setX(double x) {
propertyChangeSupport.firePropertyChange("x", this.x, this.x = x);
}
public double getX() {
return x;
}
public void setY(double y) {
propertyChangeSupport.firePropertyChange("y", y, this.y = y);
setX(y);
}
public double y() {
return y;
}
}
现在,在一个单独的类中,我定义了两个文本小部件,xText
和 yText
,它们绑定到此对象,如下所示:
DataBindingContext bindingContext = new DataBindingContext();
bindingContext.bindValue(WidgetProperties.text(SWT.Modify).observe(xText),
BeanProperties.value(HumidityScanParameters.class,"x").observe(getModel()));
bindingContext.bindValue(WidgetProperties.text(SWT.Modify).observe(yText),
BeanProperties.value(HumidityScanParameters.class, "y").observe(getModel()));
我发现,如果我更改 yText 中的文本,那么 setter 会按预期自动调用,这会同时设置 yText 和 x模型。但是,xText
未更新。这是为什么呢? firePropertyChange()
调用不应该安排更新文本吗?
谢谢, 格雷厄姆.
In order to understand how JFace databindings is working, I have a model object with two properties. Changing one property should set the other to the same value:
public class Model {
private double x;
private double y;
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
public void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
public void setX(double x) {
propertyChangeSupport.firePropertyChange("x", this.x, this.x = x);
}
public double getX() {
return x;
}
public void setY(double y) {
propertyChangeSupport.firePropertyChange("y", y, this.y = y);
setX(y);
}
public double y() {
return y;
}
}
Now in a separate class I define two Text widgets, xText
and yText
, which are bound to this object like this:
DataBindingContext bindingContext = new DataBindingContext();
bindingContext.bindValue(WidgetProperties.text(SWT.Modify).observe(xText),
BeanProperties.value(HumidityScanParameters.class,"x").observe(getModel()));
bindingContext.bindValue(WidgetProperties.text(SWT.Modify).observe(yText),
BeanProperties.value(HumidityScanParameters.class, "y").observe(getModel()));
I have found that if I change the text in yText
, then the setter is automatically called as expected, and this sets both y
and x
in the model. However, xText
is not updated. Why is this? Shouldn't the firePropertyChange()
call arrange for the Text to be updated?
Thanks,
Graham.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编译器正在优化
this.x
和this.y
的初始值,这导致PropertyChangeSupport
实例丢弃更改通知。它并不认为有什么改变。如果我引入一个像这样的临时变量:那么通知就会按照我的预期发生。
The compiler was optimising away the initial value of
this.x
andthis.y
, which led thePropertyChangeSupport
instance to discard the change notification. It didn't think anything had changed. If I introduce a temporary variable like this:then the notifications occur as I might expect.
我猜这是因为您在正确更新之前将this.x
添加到您的活动中。试试这个:公共无效setX(双x){
这个.x = x; // 更新模型!
propertyChangeSupport.firePropertyChange("x", this.x, x);
}
(参见评论;-))
I guess it's because you putthis.x
to your event before haveing it updated properly.Try this:public void setX(double x) {
this.x = x; // update the model!
propertyChangeSupport.firePropertyChange("x", this.x, x);
}
(see comments ;-) )