Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 16 - Pages: 2   [ 1 2 | Next ]
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

JGoodies: Understanding Binding - Part 1

At 12:10 AM on Mar 9, 2005, R.J. Lorimer wrote:

Overview

As of early February, the JGoodies binding framework finally reached 1.0 status. That is good news for nearly anyone who does Swing Java development, whether they know it or not. While the JGoodies Looks and JGoodies Forms frameworks are extremely valuable, I personally feel the binding framework has the potential to be the most powerful and useful in the long term. That is the beauty of JGoodies, however, each component has its place.

The binding framework is well titled. The main goal of this framework is to simplify and unify the process of connecting the state of visual components (such as buttons) with values in your application domain. Before I get too far in, let me link you to Karsten's Data Binding Presentation - it is a great supplement to understanding the intention behind the binding framework, as long as you can fill in the speech behind the slides. In addition, if you dowload the binding framework from here: https://binding.dev.java.net/ - you will be afforded a much more comprehensive set of documentation about what everything is, and when the binding framework is appropriate.

I'd love to be able to provide a short, quick tip to completely sell the binding framework. Unfortunately, I think it will take a little bit more than that. Today I want to talk about what this binding thing is all about, and hopefully given a quick run through; get you all fired up about what it does. Over the next few days, I want to get into more details about using the binding framework given real-world needs. So, what do I mean specifically by 'connecting the state of visual components with values in your application domain'? Well, more often than not, check boxes, text fields, lists, spinners, etc. correlate to values in your application domain. So, next question - what do I mean by 'application domain?'? Typically, your application will have objects that represent the data you are interacting with: Customer, Product, Meeting, Sprocket, yadayadayada. These objects are usually Javabeans (objects with fields, and getters and setters), and are usually what is called 'the domain model' - objects that model the application domain. Ok, enough verbal blathering. Let's get to the point.

The Problem at Hand

As I stated earlier, the binding framework tries to solve the problem of tying components with application objects. So, what is the problem? Well, typically if you wanted to get values from a GUI control down to an application object, you use a listener (such as an ActionListener of a checkbox). The same can be said for getting domain object changes up to the GUI controls. Typically there you would use a PropertyChangeListener or an Observer. One way or another, the goal is for changes on one side to be propagated to the other side. This pattern typically resolves to multiple listeners per property you want to synchronize (one for the way up, the other for the way down). When you get rather complicated beans, this is a cumbersome process at best. Granted, sometimes you only want to move all of the values in one direction or another when the user performs a particular behavior (such as pressing a 'Save' button or a 'Reload' button). Even still, in these cases you have at least two methods you have to write (copy up to GUI, copy down to domain), and they still require some effort to maintain.

If you don't take that route, you can always create adapters for the underlying presentation models on the various components to interact directly with your bean properties. This amounts to creating an implementation of the presentation model interface in question (e.g. ButtonModel) that interacts with the property of your bean. While this is a different approach, I'm not sure it's better. There is still a lot of overhead in this process. Both of these approaches are discussed in the binding presentation I linked to above.

The Binding Framework

Well, then how does the binding framework work? The binding framework acts as an intermediary between the view and your application model. It uses the same techniques on both sides to connect values as I just discussed -on the model side it uses observable or property change listeners depending on your choice, and on the view it adapts the presentation model. The difference is that it does so using an object (or chain of objects) in between that act as a 'universal translator' for both sides. Because the binding framework creates a single intermediary - the com.jgoodies.binding.value.ValueModel interface), both sides only have to interact with it in one common way. The distinction being that the value model always interacts with your bean, and then the presentation model implementations for buttons, text fields, and the like all interact with the value model API. So, to get a quick start for those who may be getting impatient by now, here is how to tie a boolean bean property to a checkbox:

// Example Bean... MyBean.java
import java.beans.PropertyChangeListener;
import com.jgoodies.binding.beans.*;
public class MyBean {
 // Note you don't HAVE to use this class - you can use java.beans.PropertyChangeSupport if you want.
 private ExtendedPropertyChangeSupport changeSupport = new ExtendedPropertyChangeSupport(this);
 private boolean booleanValue;
 
 public boolean getBooleanValue() {
  return booleanValue;
 }
 public void setBooleanValue(boolean newValue) {
  boolean oldValue = booleanValue;
  booleanValue = newValue;
  changeSupport.firePropertyChange("booleanValue", oldValue, newValue); 
 }
 
 public void addPropertyChangeListener(PropertyChangeListener x) {
  changeSupport.addPropertyChangeListener(x);
 }
 
 public void removePropertyChangeListener(PropertyChangeListener x) {
  changeSupport.removePropertyChangeListener(x);
 }
}
 
// Example test class - BindingTest.java
import javax.swing.*;
 
import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.beans.PropertyAdapter;
 
public class BindingTest {
 
 public static void main(String[] args) {
 
  MyBean bean = new MyBean();
  
  // property adapter is implementation of ValueModel
  // this implementation observes property changes on the bean (hence the 'true')
  PropertyAdapter adapter = new PropertyAdapter(bean, "booleanValue", true);
  // creates a JCheckBox with the property adapter providing the underlying model.
  JCheckBox box = BasicComponentFactory.createCheckBox(adapter, "Boolean Value");
  
  JFrame frame = new JFrame();
  frame.getContentPane().add(box);
  frame.pack();
  frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  frame.setVisible(true);
 }
}

In the above block of code, all I do is create a property adapter for the property we want to connect, create a component for that property adapter, and put it in our GUI. The rest is taken care of for us. Now, every time the checkbox is checked/unchecked, the 'bean' object will be updated automagically, and likewise, everytime the bean object is updated, the checkbox will be checked/unchecked automagically.

Bean Adapters

In addition to the PropertyAdapter API, there is also the BeanAdapter API which is more efficient when dealing with multiple properties on a single bean. Here is an extended example using the bean adapter:

// Example Bean... MyBean.java
import java.beans.PropertyChangeListener;
 
import com.jgoodies.binding.beans.*;
 
public class MyBean {
 // Note you don't HAVE to use this class - you can use
 // java.beans.PropertyChangeSupport if you want.
 private ExtendedPropertyChangeSupport changeSupport = new ExtendedPropertyChangeSupport(
   this);
 
 private boolean booleanValue;
 private String stringValue;
 
 public boolean getBooleanValue() {
  return booleanValue;
 }
 
 public void setBooleanValue(boolean newValue) {
  System.out.println("Boolean value set: " +newValue);
  boolean oldValue = booleanValue;
  booleanValue = newValue;
  changeSupport.firePropertyChange("booleanValue", oldValue, newValue);
 }
 
 public void addPropertyChangeListener(PropertyChangeListener x) {
  changeSupport.addPropertyChangeListener(x);
 }
 
 public void removePropertyChangeListener(PropertyChangeListener x) {
  changeSupport.removePropertyChangeListener(x);
 }
 
 public String getStringValue() {
  return stringValue;
 }
 
 public void setStringValue(String newValue) {
  System.out.println("String value set: "+  newValue);
  String oldValue = stringValue;
  this.stringValue = newValue;
  changeSupport.firePropertyChange("stringValue", oldValue, newValue);
 }
}
 
// Example test class - BindingTest.java
import java.awt.GridLayout;
 
import javax.swing.*;
 
import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.beans.BeanAdapter;
import com.jgoodies.binding.value.ValueModel;
 
public class BindingTest {
 
 public static void main(String[] args) {
 
  MyBean bean = new MyBean();
  
  // Bean adapter is an adapter that can create many value model objects for a single 
  // bean. It is more efficient than the property adapter. The 'true' once again means 
  // we want it to observe our bean for changes.
  BeanAdapter adapter = new BeanAdapter(bean, true);
  
  ValueModel booleanModel = adapter.getValueModel("booleanValue");
  ValueModel stringModel = adapter.getValueModel("stringValue");
  // creates a JCheckBox with the property adapter providing the underlying model.
  JCheckBox box = BasicComponentFactory.createCheckBox(booleanModel, "Boolean Value");
  JTextField field = BasicComponentFactory.createTextField(stringModel);
  JFrame frame = new JFrame();
  frame.getContentPane().setLayout(new GridLayout(2,1));
  frame.getContentPane().add(box);
  frame.getContentPane().add(field);
  frame.pack();
  frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  frame.setVisible(true);
 }
}

In this example I have added print statements to the bean just to show you how well the binding framework works. Pretty cool, huh?

The last thing I want to mention today in my breakneck tutorial is the two different factory classes - com.jgoodies.binding.adapter.BasicComponentFactory - which you have seen me use here, and com.jgoodies.binding.adapter.Bindings which I haven't mentioned. Where as the BasicComponentFactory creates components and then binds them to a value model, the Bindings class simply does the latter - it lets you provide your own widget implementation:

BasicComponentFactory.createCheckBox(booleanModel); 
// --> equates to:
JCheckBox checkbox = new JCheckBox();
Bindings.bind(checkbox, booleanModel);

The reason I bring this up is because it may be important for you to bind to already constructed widgets, or maybe custom implementations of widgets. In other words, if your application requires you use your own widgets, you can use the Bindings class to hook value models up just the same.

Now, I realize I have only very broadly brushed this subject. I hope, however, I have given you some insight on how to bind bean properties to GUI controls. I plan to cover some more complicated topics over the coming tips, so stay tuned!

Until next time,

R.J. Lorimer
rj -at- javalobby.org
http://www.coffee-bytes.com

1 . At 11:05 AM on Mar 10, 2005, Nick Christy wrote:
  Click to reply to this thread Reply

Re: JGoodies: Understanding Binding - Part 1

Very interesting. Now, the next step is to introduce a orm such as hibernate to the JGoodies binding. Then the GUI is bound to your application domain, but your domain is bound to your database as well. Do you see JGoodies tieing this into their framework so that you only need to introduce one framework into your application?

-Nick
http://nickchristy.blogspot.com
http://www.nickchristy.com
2 . At 11:39 AM on Mar 10, 2005, R.J. Lorimer wrote:
  Click to reply to this thread Reply

Re: JGoodies: Understanding Binding - Part 1

Nick,

The binding framework, form framework, and looks framework are all actually part of the 'for-pay' JGoodies Swing Suite which does all of these things, and much more in an integrated package.

With respect to Hibernate, no, I don't think they will directly integrate it - the nice thing about this is since 'MyBean' is just a standard object, Hibernate works with it quite well. The benefits of tying Hibernate to the model through the binding framework would probably be minimal.

If you are interested in true integration with other tools like that, I would consider looking at the Spring Rich-Client Project .
Best, R.J. Lorimer
3 . At 2:44 AM on Mar 11, 2005, john Lee wrote:
  Click to reply to this thread Reply

Re: JGoodies: Understanding Binding - Part 1

I think binding will provide easy clen nice view part(presentation).

Hibernate will provide ur model stuff.

you can really build true higer level MVC architecture.
4 . At 7:45 AM on Mar 11, 2005, Karsten Lentzsch wrote:
  Click to reply to this thread Reply

Thanks and Some Additions

Hello,

I'm glad to see that and how you describe the general data binding problems and concepts, and the implementation provided by the JGoodies Binding.

Data binding is at the core of every Swing application; we all need to connect domain object properties to Swing component state. Hence it's worth to learn the patterns, concepts and solutions available - even if you copy data back and forth, and even if you use don't use the JGoodies implementation. And I recommend to browse and study the desktop patterns provided by Martin Fowler in his further "Patterns of Enterprise Application Development".

Regarding your example domain bean, let me just add, that there's a simple way to turn a general domain class into an observable bean that can provide bound properties (those who fire changes). Just extend class com.jgoodies.binding.beans.Model. It includes a property change support and a set of methods to fire changes.

Best regards,
Karsten Lentzsch
5 . At 9:27 AM on Mar 11, 2005, Karsten Lentzsch wrote:
  Click to reply to this thread Reply

Binding and Persistency

The JGoodies Binding has been designed to work with different domain architectures, coding styles, update mechanisms, and persistency solutions.

The Binding is built around the ValueModel interface that describes an object that can read and write a value, and that can optionally notify observers about value changes. This interface is flexible enough to be adapted to the vast majority of domain object implementations.

On top of that the JGoodies Binding adds convenience classes to work with bean-like classes, classes that follow the coding conventions for accessing properties as described in the Java Bean specification. Basically you just need to provide a getter method, an optional setter, and you may send change notifications. These relaxed requirements are flexible enough to work with all ORMs I'm aware of, including Hibernate.

I plan to add a larger demo to the Swing Suite and later to the Binding tutorial that combines Hibernate with the JGoodies Binding. Hibernate and the Binding work well with Beans; Hibernate connects the bean-properties to the database, while the Binding connects them to the UI components.

Kind regards,
Karsten Lentzsch
6 . At 9:42 AM on Mar 11, 2005, R.J. Lorimer wrote:
  Click to reply to this thread Reply

Re: Thanks and Some Additions

Karsten,

Thanks for the updates!

You're giving away all of my secrets though - heh. In all seriousness, I was going to discuss the model class, the preferences adapter, and some of the extras in a quick part 4 for today ;) .

Anyway, thanks again!

R.J.
Best, R.J. Lorimer
7 . At 10:05 PM on Mar 11, 2005, Scott Tavares wrote:
  Click to reply to this thread Reply

Re: Thanks and Some Additions

> And I recommend to browse and study
> the desktop patterns provided by Martin Fowler in his
> further "Patterns of Enterprise Application
> Development".

Where can I find this information? I've checked Martin's web site and did not find anything except the first P EAA book.

TIA
8 . At 12:53 AM on Mar 12, 2005, john Lee wrote:
  Click to reply to this thread Reply

Re: Thanks and Some Additions

http://martinfowler.com/eaaDe
9 . At 7:41 AM on Mar 12, 2005, Scott Tavares wrote:
  Click to reply to this thread Reply

Re: Thanks and Some Additions

Thanks but... that link seems to be broken.
10 . At 4:09 PM on Mar 12, 2005, john Lee wrote:
  Click to reply to this thread Reply

Re: Thanks and Some Additions

sorry I have sticky finger.

http://martinfowler.com/eaaDev/
11 . At 5:55 PM on Mar 12, 2005, David Coldrick wrote:
  Click to reply to this thread Reply

Re: Thanks and Some Additions

Try http://martinfowler.com/eaaDev/
12 . At 1:23 PM on Mar 14, 2005, Mark N DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: JGoodies: Understanding Binding - Part 1

You meantion in this part custom components. I am using JCalendar for dates. A quick clue on how to use that?

Thanks.
13 . At 8:05 AM on Mar 15, 2005, Mark N DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: JGoodies: Understanding Binding - Part 1

Figured it out. I was passing the wrong thing as part of the first parameter.
14 . At 5:23 AM on Aug 3, 2006, patrick boulay wrote:
  Click to reply to this thread Reply

Re: JGoodies: Understanding Binding - Part 1

nice tutorial
simply would like to say that I had to use
the following patch to have the String binding working ....
anyway as I jave custom widgets ... ;o)



//JTextField field = //BasicComponentFactory.createTextField(stringModel);

JTextField field = new JTextField();
Bindings.bind(field, stringModel);

thread.rss_message