Updating Bindings when underlying object changes

Swing Designer allows you to quickly create the frames, panels, dialogs, applets and other UI elements that comprise Java Swing applications.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Updating Bindings when underlying object changes

Postby IanHarac » Sun May 17, 2009 10:55 am

Greetings!

I recently upgraded to the bindings-supported version of Windowbuilder, and I love it. I've been redoing all my GUIs to use it. I recently found an issue, though, which I hope can be resolved.

Almost all the data fields on my main form reference one object, called "Environment", which I now bind to. All the properties in that object fire change events, so my bound controls update properly. However, the Environment object itself can be changed by the user, loading a marshalled version of the object from disk. I am of course using get/set in the form to set the Environment object, but when it changes, the bound controls do not update to the field values in the new object unless I call InitDataBindings() again, and I do not think that's the right thing to do.

That is:
Form is created, and it loads a default Environment object from disk. Code is something like "setEnvironment(LoadEnvironment(FileName))";

After this environment has been loaded, InitDataBindings is called for the first time.

The form is displayed and binding works.

The user then selects "Load Environment" from the menu. Another environment is loaded, something like this:
Environment new=LoadEnvironment(FileName);
if(new!=null) setEnvironment(new);

And then the binding doesn't update.

If I call InitDataBindings in setEnvironment, it does work, but, again, I don't think that's right. I've come up with a number of baroque ideas I think will fix it, but they all seem more complex than should be required; I think there's something very simple and obvious I'm missing. Please help?

Thanks.
IanHarac
 
Posts: 27
Joined: Wed Jan 10, 2007 7:04 am

Re: Updating Bindings when underlying object changes

Postby Eric Clayberg » Sun May 17, 2009 12:14 pm

IanHarac wrote:Almost all the data fields on my main form reference one object, called "Environment", which I now bind to. All the properties in that object fire change events, so my bound controls update properly. However, the Environment object itself can be changed by the user, loading a marshalled version of the object from disk. I am of course using get/set in the form to set the Environment object, but when it changes, the bound controls do not update to the field values in the new object unless I call InitDataBindings() again, and I do not think that's the right thing to do.

If you have actually replaced the main object that your UI is bound to, you need to dispose of the existing bindings and call initDataBindings() again. Something like this...

Code: Select all
  public void setEnvironment(Environment environment) {
    if (m_bindingContext != null) {
      m_bindingContext.dispose();
    }
    this.m_environment = environment;
    m_bindingContext = initDataBindings();
  }
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Updating Bindings when underlying object changes

Postby IanHarac » Sun May 17, 2009 12:42 pm

Thanks, I will try that.
IanHarac
 
Posts: 27
Joined: Wed Jan 10, 2007 7:04 am

Re: Updating Bindings when underlying object changes

Postby Eric Clayberg » Tue May 19, 2009 6:13 pm

IanHarac wrote:Thanks, I will try that.

I just realized that the code I posted was for SWT data binding. For Swing, there is no bindingContext object created that you can "release".
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to Swing Designer

Who is online

Users browsing this forum: No registered users and 1 guest