Tuesday, April 29, 2008

JavaBuilder for Swing : now with two-way binding support!

I am happy to announce the first working support for binding in JavaBuilders, with full support for two way binding!

I realised I had the wrong idea initially (i.e. putting binding info in the YAML file). Instead in belongs on the Java side. This is especially important once I start adding support for EL binding (that's scheduled for later this week).

Here's the sample YAML build file:
http://code.google.com/p/javabuilders/source/browse/trunk/org.javabuilders.swing/samples/org/javabuilders/swing/samples/binding/BindingFrame1.yaml
and the corresponding Java file:
http://code.google.com/p/javabuilders/source/browse/trunk/org.javabuilders.swing/samples/org/javabuilders/swing/samples/binding/BindingFrame1.java

And here is the resulting window when it opens:

The initial field values get populated from the Java side via the two-way binding (see the constructor code).

We modify it on the screen, press OK (the OK button is wired from YAML to execute the save() method) and we get:
Voila!

This is starting to make Swing programming look really a lot like JSF where the YAML file has all the layout/creation info and the corresponding Java class looks like a JSF backing bean, with just the relevant properties and event methods.

New code drop with this features to celebrate this:
http://javabuilders.googlecode.com/files/javabuilder.swing.0.0.2008-04-30.zip

In other news, the @Name annotation was changed to @Alias to more clearly indicate what it really does.

Plans for the next short future:
a) support for EL binding, e.g.

@Bind("{fName.text} {lName.text}")
private String fullName;

although this would be read-only of course (from YAML to Java, not two-way).

b) initial investigation of integration with a validation framework (maybe Hibernate Validators, if it's not JPA specific)

c) finished the MouseListener handler to provide support for the following events:

onMouseClicked
onMouseDoubleClicked
onMouseRightClicked
onMouseEnter
onMouseExited
onMousePressed
onMouseReleased

See sample:
http://code.google.com/p/javabuilders/source/browse/trunk/org.javabuilders.swing/samples/org/javabuilders/swing/samples/TestFrame.yaml

Stay tuned!

11 comments:

Jacek Furmankiewicz said...

Forgot to mention: all the binding relies on the Beans Binding framework of course:
https://beansbinding.dev.java.net/

Andres Almiray said...

Impressive. I assume it also supports a way to convert a value from boolean to String and back (for example).

Don't forget about mouse wheel events ;-)

Jacek Furmankiewicz said...

I believe so...yes, I will do the mouse move and wheel events next.

More good stuff coming in the future. I am having too many great ideas...too little time..this daily coding till midnight is not good for my pale appearance! :-)

Jacek Furmankiewicz said...

The boolean stuff works now on a rudimentary basis, e.g.:

@Bind(source="fName",property="enabled")
private boolean inputAllowed = true;

in constructor:

setInputAllowed(false);

and the fName.enabled property gets set to false and the field gets disabled.

However, since only one instance of @Bind is allowed you can't bind the same property to multiple targets (e.g. one property to control the enabled property of all controls).

But that will come once I add the ability to add binding in the YAML file too.

Instead of doing it like javaFX (i.e. right in the property with some "bind:" keyword) I think I want to keep it cleaner and just create a separate node for YAML-side bindings, e.g.:

content:
- JTextField: {name: fName}
- JTextField: {name: lName}
binding:
- fName.enabled: inputAllowed
- lName.enabled: inputAllowed

but I still need to do more thinking about that, which direction goes where, it is two-way, or read-only, etc, etc...

Gregg Bolinger said...

Looking good, again. Your losing cred with me the more you mention its likeness to JSF. You realize nearly every web framework these days binds this way, right? :)

Jacek Furmankiewicz said...

I am not even at an 0.1 release and I am losing cred already...nice... :-)

codecraig said...

What layout does it use? I sometimes see "MigLayout" and sometimes I don't see any specific name given. Just curious, I'd like to understand the layout code better.

Does this work with Java 5 or Java 6?

Are you going to create a project page for this to centralize the information? (i.e. required libraries [such as binding], how-to, tutorials, tid-bits posted in comments?)

thanks

Jacek Furmankiewicz said...

By default it uses MigLayout. The "layout" node is an alias for it, since it will be the default for the entire approach.

So:
MigLayout:

and

layout:

mean exactly the same thing.

The main page is:
http://javabuilders.org

and definitely there will be docs, tutorials, etc

Jacek Furmankiewicz said...

Forgot: works with Java 5. I changed all the code two weeks ago to run under it and changed my default JDK during development to be 5 as well.

As far as dependencies, they're in the lib folder of the zip file, it's mostly some Apache Commons, the YAML library, BeansBinding and MigLayout. I am trying to keep it as small as possible

Richard said...

Jacek,

Very cool. Are you programmatically controlling MigLayout and adding the components at runtime?

If so, there would appear to be some overlap with my own project Metawidget and its Layout classes.

Perhaps if you get time you could take a look at it. I'd appreciate your feedback.

Regards,

Richard.

Anonymous said...

You will get a beautiful cloth which in the game if we want to need the beautiful cloth, we can use our own Scions Of Fate gold to buy. The one I owned on my character is one of my friends sent to me the necessary SOF gold. Sometimes we can share the trophy as the necessary Scions Of Fate money together, and we do quest together. I do not have enough confidence and cheap SOF gold about my weak memory. First I have to buy sof gold to improve my pet which I have a lovely leopard in this game.