Netbeans 绑定字段不更新 GUI
我已经设置了一个 Netbeans 表单,其中的文本字段绑定到 bean 属性。绑定应该是双向的,但仅适用于单向。对 GUI 中字段的更新会更新 Bean,但如果更新 Bean,则 GUI 不会更新。
对于每个属性,Netbeans 生成的代码如下所示:
binding=org.jdesktop.beansbinding.Bindings.createAutoBinding(
org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, crmmast1,
org.jdesktop.beansbinding.ELProperty.create("${fname}"), lname,
org.jdesktop.beansbinding.BeanProperty.create("text"));
bindingGroup.addBinding(binding);
我的 bean 添加了属性更改支持:
private PropertyChangeSupport changeSupport = new
PropertyChangeSupport(this);;
public void addPropertyChangeListener (PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(
PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
public void firePropertyChange (String propertyName,
Object old, Object newObj) {
changeSupport.firePropertyChange(propertyName, old, newObj);
}
在我拥有的属性的 sett 中:
public void setFname(String newName) {
firePropertyChange("fname", fname, newName);
this.fname = newName;
}
当我更新底层 bean 时,bean 会更新,但文本字段不会更新(尽管我已经验证了这一点)正在触发属性更改事件。
I have setup a Netbeans form with a text-field bound to a bean property. The binding is supposed to be two-way, but only works one-way. Updates to the field in the GUI update the bean, but if the bean is updated, the GUI does not update.
The Netbeans generated code looks like this for each property:
binding=org.jdesktop.beansbinding.Bindings.createAutoBinding(
org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, crmmast1,
org.jdesktop.beansbinding.ELProperty.create("${fname}"), lname,
org.jdesktop.beansbinding.BeanProperty.create("text"));
bindingGroup.addBinding(binding);
My bean has property change support added:
private PropertyChangeSupport changeSupport = new
PropertyChangeSupport(this);;
public void addPropertyChangeListener (PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(
PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
public void firePropertyChange (String propertyName,
Object old, Object newObj) {
changeSupport.firePropertyChange(propertyName, old, newObj);
}
Within the sett for the properties I have:
public void setFname(String newName) {
firePropertyChange("fname", fname, newName);
this.fname = newName;
}
When I update the underlying bean, the bean updates but the textfield does not (even though I have verified that a property change event is being fired.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
遇到了同样的问题。真烦人。我花了很长时间才意识到有关 AutoBinding 的一些事情:它似乎监听在 createAutoBinding 调用中指定的 Source 和 Target 之间建立的 Property 上下文中的操作。这就是为什么在 JTextField 中输入文本会毫无问题地更新您的 bean 属性。可以说,它位于同一个绑定“轨道”上。
这意味着,当您尝试直接从另一个源通过 setX() 调用来更新 JavaBean 时(就像我使用 JFileChooser 所做的那样),自动绑定的成员看不到它,即使调用了 firePropertyChange 方法也是如此。
这样做不起作用:
因此,我没有执行该调用,而是确保绑定附加了一个“名称”,该名称被放置为 createAutoBinding() 调用中的最后一个参数。我在 Netbeans Matisse 属性编辑器的“高级”选项卡下的“绑定”弹出工具中执行了此操作。在下面的示例中,它被命名为“fileLocation”。
让我进入绑定“rail”的调用,以便 JTextField 将使用我选择的文件进行更新,如下所示:
我必须使用全局绑定组中的 getBinding() 和 getSourceProperty() 来拉起它,因为 Matisse 锁定了直接访问原始绑定,并且我不应该编辑它,因为代码是自动生成的。如果您手动编辑代码,那么您始终可以确保有一个可用的名称来直接访问 Property 对象。
无论如何,让“外部影响者”对 bean 和 JTextField 进行操作的关键是对 Binding Property 本身执行更改。
Ran into the same problem. Really annoying. It took me a long time to realize something about the AutoBinding: it appears to listen to actions within the Property context established between the Source and the Target that you specify in the createAutoBinding call. This is why entering text in the JTextField updates to your bean property with no problem. It's on the same binding 'rail' so to speak.
What this means is that when you attempt to update your JavaBean via its setX() call directly from another source, like I was doing with a JFileChooser, the members of the autobinding don't see it, even though the firePropertyChange method is called.
doing this does not work:
So, instead of doing that call, what I instead did was make sure the Binding had a 'name' attached to it, which gets placed as the last parameter in the createAutoBinding() call. I did this in the Netbeans Matisse Property editor Bind popup Tool under the Advanced tab. In the example below, it is named "fileLocation".
The call that got me on the binding 'rail' so that the JTextField would update with my selected file looked like the following:
I had to pull this up with getBinding() and getSourceProperty() from the global binding group because Matisse locks out direct access to the original binding, and I am not supposed to edit it since the code is autogenerated. If you are hand editing your code, then you could always ensure that you have an available name to access the Property object directly.
In any case, the key to get an 'outside influencer' to work on the bean and the JTextField is to perform your changes on the Binding Property itself.
最近我尝试使用 netbeans 创建 Swing GUI,我想知道完全相同的问题(因此找到了您的帖子)。
您的解决方案应该有效。只需记住两件事:
changeSupport.firePropertyChange
之前更改code> 正在被调用。请参阅:http://wiki.netbeans.org/BestPracticesWithJPAAndBeansBinding一旦遵守这两件事,一切都应该有效。我还使用 JFileChooser 更改文本字段,无需手动设置绑定的属性(如其他答案所建议)。
Recently I tried Swing GUI creation with netbeans and I was wondering about exactely the same issue (therefore found your post).
Your solution should work. There are only two things to keep in mind:
changeSupport.firePropertyChange
is being called. See: http://wiki.netbeans.org/BestPracticesWithJPAAndBeansBindingOnce these two things are obeyed, everthing should work. I also used JFileChooser to change a text field and there is no need to set the properties of the binding manually (as suggested by the other answer).