Swing Data Binding 1.0 Released
Join the DZone community and get the full member experience.Join For Free
Lately I've used Beans Binding (JSR 295) in my projects, but it has always given me trouble. I felt that there was need for a simple and clean approach to data binding, so I decided to create Swing Data Binding. Version 1.0 was released today.
I wanted a data binding solution addressing the following key points:
- Easy to use and easy to extend to support custom ui elements and data types
- Fast and light
- Support PropertyChangeSupport if you want it
- Swap out your model objects for active bindings (rebind)
- Unbind should remove all listeners
- Supply user feedback with ease
- Out-of-the-box support for JXDatePicker and Joda Time
- Provide a fluent interface if that's your bag
Especially rebinding and swapping out the model for existing bindings seems to be a pain point of other existing solution. When using Beans Binding (JSR 295), I would often end up with hundreds of listeners on my model after the same object had been edited multiple times. Also, I often needed to refresh the model object after it had been changed on the server, and I did not want to re-create all ui elements and bindings.
<dependency> <groupId>no.tornado.databinding</groupId> <artifactId>databinding</artifactId> <version>1.0</version> </dependency> <!-- Optional support for JXDatePicker and Joda Time --> <dependency> <groupId>no.tornado.databinding</groupId> <artifactId>jxdatepicker-support</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>no.tornado.databinding</groupId> <artifactId>jodatime-support</artifactId> <version>1.0</version> </dependency>
Let's start by instantiating a fictive Person model object and bind it's username property to a JTextField:
Person person = new Person(); JTextField usernameField = new JTextField(); BindingGroup bindings = new BindingGroup(person); bindings.add(usernameField, "username"); bindings.bind();That's all you need to get started! Read the documentation to learn how to configure and extend different binding/validation strategies, how to provide feedback, and how to use Swing Data Binding in a NetBeans Platform Application.
There is also an example application available:
Although most of the code deals with Swing/UI specifics, the setupBinding() method shows some more binding examples.
If you find bugs, wants a specific converter or validator included, or have suggestions to improve Swing Data Binding, please send me an email at email@example.com. I hope you will enjoy the easiest data binding framework for Swing!
Opinions expressed by DZone contributors are their own.