Hi

I want to create a simple bi-directional variable binding in java. assume having a String and a jTextField

String stringVariable;
JTextField jTextField;

I bound the jTextField.text property to the stringVariable using this code :

org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${stringVariable}"), jTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));

bindingGroup.addBinding(binding);
bindingGroup.bind();

any thing is OK whenever I change the jTextField text , changes applied to the stringVariable

BUT

as I change the stringVariable value , no changes seems to be occurred in jTextField

please help me how to solve this problem
Thanks

Dani AI

Generated

Short answer: the binding needs to watch a JavaBean property that fires change events. Binding libraries like org.jdesktop.beansbinding work with JavaBean properties (BeanProperty / ELProperty) and start tracking values only after the binding is realized; they do not magically observe arbitrary local/field assignments. (doc.formdev.com)

A practical fix is to wrap the String in a small JavaBean and fire a PropertyChange from the setter. Example pattern:

public class Model {
    private String text;
    private final java.beans.PropertyChangeSupport pcs = new java.beans.PropertyChangeSupport(this);

    public String getText() { return text; }

    public void setText(String newText) {
        String old = this.text;
        this.text = newText;
        pcs.firePropertyChange("text", old, newText);
    }

    public void addPropertyChangeListener(java.beans.PropertyChangeListener l) {
        pcs.addPropertyChangeListener(l);
    }

    public void removePropertyChangeListener(java.beans.PropertyChangeListener l) {
        pcs.removePropertyChangeListener(l);
    }
}

Bind your UI control to the model's text property (EL/BeanProperty like ${text} or BeanProperty.create("text")), then call the binding group's bind() so the initial copy is made and change-tracking starts. The standard helper for firing bound-property events is PropertyChangeSupport; Swing also provides SwingPropertyChangeSupport if you want notifications guaranteed on the Event Dispatch Thread. (docs.oracle.com)

Quick troubleshooting checklist:

  • Make sure you bind to the bean instance (model), not a raw field or an object that doesn't expose property-change listeners. (doc.formdev.com)
  • Ensure the setter calls firePropertyChange(...) (no event = no notification). (docs.oracle.com)
  • Call bindingGroup.bind() after the model is set up so the binding starts tracking. (doc.formdev.com)

This follows 's listener/encapsulation suggestion and 's advice to bind to an object's property path; it also matches 's point that this is a third-party feature (not part of core Java) and therefore depends on bean/event conventions. (formdev.com)

Recommended Answers

All 7 Replies

Then, seemingly, this org.jdesktop package is not repainting the textfield after the variables value changes.

Sounds like a question for where ever you got this this "org.jdesktop" package. You may be doing something wrong. In any case this "binding" is not a standard Java feature, but rather a specific "feature" of this third party library you're using, so, logically, you should be asking them.

So
1. is there any standard java notation having the binding idea?
2. do you have any other ideas?

thanks

So
1. is there any standard java notation having the binding idea?

I thought I just said no to that.

2. do you have any other ideas?
thanks

Yeah, use setText() and getText.

There is no reason that the setting of the value, and the updating of the textfield (and vice versa) can't take place in the same method so that doing both is still just a single method call.

Off course

but the main idea behind binding is to automatic change tracing , and no manually change management.

I need something ,having the idea!

This is why accessor methods are such a good idea. Your String should only be modifiable via a public set(...) method. That method can then take care of any side efects that are necessary. The "standard" Java approach is to use a Listener / Observer pattern; in this case the set(...) method can notify all registered listeners that the String value has changed. This exactly mirrors the way that jTextFields notify listeners of a change in the label's text.

ps: There's a deeper level of analysis here, if you're interested.

Java Strings are immutable. You cannot change the value of a Java String. You can change the value of a Java String variable, but variables are not objects. To use the Listener pattern you have to package your String within an Object that allows its value to be changed (even if that value is a String variable!). This typically is not an issue because the thing being observed is usually much more than just a single String.
I'm not familiar with the beansbinding package you are using, but for it to work the way you exepct it would need to use something like tha java debugging interfaces to trigger an event when the String variable was changed. Sounds a bit tacky to me.

The way binding normally works is that you bind UI controls to properties of an object/instance. This is done because in most of the cases there is no way you can detect changes to standalone variables. Simply put, you bind properties of a model to UI controls and leave the updation of display to the events raised by the binding implementation.

In your case having something like ${object.stringVariable} instead of ${stringVariable} might just do the job; read the API of the library you are using for more details.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.