So where's binding in Java at? Don't 'ya just hate that? I'm not sure if using bad grammer even makes a point anymore because so many of us don't recognize even obvious butchering of our language (not that I couldn't benefit from a refersher at this point in my life). I guess I could have said "what's up with binding in Java these days?"

Lately I have been doing my Java programming in Eclipse and NetBeans after loving and using JBuilder just about since it was released (although I have heard I won't be able to do without the Eclipse-based JBuilder which I can never remember is code-named Peleton). I understand that NetBeans 6.0 will have binding but I'm not sure what that will look like and after attending Java One and reading about binding, I think I'm clueless. If you're doing some type of binding now and you're not using the stuff from SwingLabs (assuming you can still find it) or JGoodies, I'd like to know what's working for you.

When I first found JGoodies stuff, I was intrigued by the forms. In fact before Matisse made its debut in NetBeans, I thought I would settle on JGoodies (I say settle because I tend to be an IDE drag-and-drop GUI builder kind of guy). I suffered through trying to build Swing UIs with JBuilder (layout manager within layout manager and so on) and as a general rule gave up coding UI's for production systems by hand back before Clinton "felt our pain." Seeing Matisse in NetBeans previewed at JavaOne '05 made a believer out of me (OK so I can be gullible but I prefer to think of it as optimisitic). It reminded me of the Delphi launch; this stuff still makes me feel like a kid.

So I've just about given up on the SwingLabs binding stuff because I can't tell where it's going for now but I suspect it along with what Scott Violet demoed at JavaOne is probably closer to what will end up in the language and NB 6.0 than the JGoodies stuff. But if you're like me and you need a solution now, the JGoodies stuff is nice and does not have too steep of a learning curve. First you have to get past the idea that your beans must support bound properties (PropertyChangeSupport). Because this is an optional part of the spec, I find it a bit funny that there are what seem to be heated debates over it. If you don't have an easy way to make these changes to your beans, check out the AOP stuff at DamnHandy. There's also HandyAspects on java.net. If nothing else this is a nice little diversion which explains why those of us who are not taking advantage of AOP (myself included) might do so.

Assuming you get past that hurdle, JGoodies binding can make you very productive very quickly, yes even using NetBeans. For me the key was the little tip about how to add Custom Creation Code. This is one of those features I stumbled across and told myself I would need that someday soon and promptly forgot about it which, by the way, is one of the main reasons I am trying to blog again. Working under the assumption that this stuff actually shows up on google's radar, someone might actually benefit from it. If not at least it's a personal diary of stuff I've otherwise not recall.

After dropping your components on the form, you can use the Custom Creation Code feature to make a call to one of BasicComponentFactory's methods to make bind it to a ValueModel. This is pretty slick stuff. I'll put up a specific example if anyone asks (or on my own once I get back to that project). As for this weekend there's some yardwork and household issues that need my attention.

Comments

xclef said…
Thanks a lot for your post! Very useful overview !
I'm currently investigating some free Swing validation solutions.
http://x-c-l-e-f.blogspot.com/2006/11/java-swing-bindingvalidation-short.html

Could you please post examples about the jgoodies+"Custom Creation Code" technique you have used ?
Unknown said…
I have (at least partially) abandoned the JGoodies stuff because of the (web services) design for a couple of swing client projects. That's not to say JGoodies is bad but the propertychangesupport issue can be a problem (again if someone is interested in this, I'll talk more about it).

On the NetBeans "custom creation code" I mentioned previously, implementing goes something like this:

1. create a presentation model for the bean to which the component(s) will be bound like this:

presentationModel = new PresentationModel(myBean);

Remember the bean must implement PropertyChangeSupport. Here's MyBean:

public class MyBean {
public MyBean() {
name = new String("John Smith");
age = new Integer(100);
choice = new Integer(0);
}

private String name;
private Integer age;
private Integer choice;

public String getName() {
return name.toString();
}

public void setName(String name) {
String oldVal = this.name;
this.name = name;
firePropertyChange("name", oldVal, name);
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
Integer oldVal = this.age;
this.age = age;
firePropertyChange("age", oldVal, age);
}
public Integer getChoice() {
return choice;
}

public void setChoice(Integer choice) {
Integer oldVal = this.choice;
this.choice = choice;
firePropertyChange("choice", oldVal, choice);
}
public void addPropertyChangeListener(java.beans.PropertyChangeListener listener) {
pcs.addPropertyChangeListener(listener);
}

public void addPropertyChangeListener(String propertyName,
java.beans.PropertyChangeListener listener) {
pcs.addPropertyChangeListener(propertyName, listener);
}

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

public void removePropertyChangeListener(String propertyName,
java.beans.PropertyChangeListener listener) {
pcs.removePropertyChangeListener(propertyName, listener);
}


private void firePropertyChange(String propertyName,
Object oldValue,
Object newValue) {
if (oldValue == null && newValue != null || !oldValue.equals(newValue)) {
this.pcs.firePropertyChange(propertyName, oldValue, newValue);
}
}
private PropertyChangeSupport pcs = new PropertyChangeSupport(this);

}

2. now select a given component (in this example I am using a JTextField which I have named "tfName")
3. go to the "Code" tab of the properties inspector
4. click on the elipsis button on the "Custom Creation Code" property
5. enter the following code:
BasicComponentFactory.createTextField(presentationModel.getModel("name"));

We have now bound the "name" property of myBean to the text field. Because of JGoodies support/use of PropertyChangeSupport, when the bean changes, the UI component will be changed and vice-versa.

I must add that as time passes, I am really looking forward to JSR-295 becoming a reality. While I like the fact that there are multiple solutions for binding and they are relatively convenient, it should be a complete "language thing" and I am glad to know it will. :)

Popular posts from this blog

ClassCastException: JAXB

Minishift on HyperV

Parallel to Lincoln's angry letters never sent concept...