开发者

Netbeans Bound Field not updating GUI

开发者 https://www.devze.com 2023-03-29 00:53 出处:网络
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

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 ch开发者_Go百科ange 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.


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:

myBean1.setFileLocation(fileChooser.getSelectedFile().getAbsolutePath());

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".

binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(
org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE,
 myBean1, 
org.jdesktop.beansbinding.ELProperty.create("${fileLocation}"), 
jTextField12, 
org.jdesktop.beansbinding.BeanProperty.create("text"), 
"fileLocation");
bindingGroup.addBinding(binding);

The call that got me on the binding 'rail' so that the JTextField would update with my selected file looked like the following:

            Binding fileBind = bindingGroup.getBinding("fileLocation");
            Property fileProp = fileBind.getSourceProperty();
            fileProp.setValue(myBean1, fileChooser.getSelectedFile().getAbsolutePath());

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.


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:

  1. You will have to provide the correct prorperty name once you propagate bean changes (fname in your case should be ok)
  2. The actual bean property has to be changed before changeSupport.firePropertyChange is being called. See: http://wiki.netbeans.org/BestPracticesWithJPAAndBeansBinding

Once 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).

0

精彩评论

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

关注公众号