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

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.