Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Beans Binding: A Java Data-Binding Solution with a Serious Problem : Page 2

Data binding simplifies how you sync a data object's properties with their visual representations, and Beans Binding for Java (JSR 295) aims to deliver a default data-binding specification for the Java platform. Find out how to use Beans Binding and work around its onerous overhead.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Creating Bindings
Now that you've seen the basic building blocks, you can create some actual bindings between two object properties. All the magic happens in the Bindings class, which has a set of static methods that returns a Binding object:

MyPOJO pojo = new MyPOJO(); JTextField field = new JTextField(); //bind a POJO's first name to a JTextField's text property BeanProperty<MyPOJO, String> fNameProperty = BeanProperty.create("firstName"); BeanProperty<JTextField, String> textProperty = BeanProperty.create("text"); Binding<MyPOJO, String, JTextField, String) firstNameBinding = Bindings.createAutoBindings(UpdateStrategy.READ_WRITE, pojo, fNameProperty, field, textProperty); //perform actual binding firstNameBinding.bind();

As you may have guessed, Bindings.createAutoBindings() is the important method; it takes all the pre-defined inputs and creates an actual binding definition for them. It is not active until you actually call bind() on it. That lets you define a binding and then customize the binding definition further, such as by adding custom converters or validators.

Converting Data
To convert data between the input type (usually just a String in a textbox or some other UI control) and the destination property (e.g., Date), you need to create a Converter object (passing it the source and target data types via generics) and then set it on your Bindings object. Here's an example:

dateBinding.setConverter(new Converter<Date, String>() { @Override public String convertForward(Date value) { return dateFormat.format(value); } @Override public Date convertReverse(String value) { try { return dateFormat.parse(value); } catch (ParseException e) { return Calendar.getInstance().getTime(); } } });

Adding Validators
To add a validator, you need to extend the Validator object and in case of an error return a Result with the details:

dateBinding.setValidator(new Validator<Date>() { @Override public org.jdesktop.beansbinding.Validator.Result validate(Date value) { if (value.getTime() > Calendar.getInstance().getTimeInMillis()) { return new Result(null, "Date must be less than current date/time"); } return null; } });

Creating Observable Lists
The collections included in the default JDK are not observable, which means that adding or removing elements from them does not fire any events that would instruct Beans Binding to update the bound targets. Fortunately, Beans Binding ships with an ObservableCollections factory object that can create such events from existing, regular collections:

ObservableList<Employee> employees = ObservableCollections.observableList( new ArrayList<Employee>());

You would then use the observable lists for the actual binding of collections.

Using the Swing-Specific Binding Utilities
Many existing Swing controls do not provide the proper getters and setters in their APIs to easily bind JavaBeans to them. In order to work around this, Beans Binding provides a Swing-specific extension class called SwingBindings. It contains some additional methods that allow you to easily bind a list to a JTable, for example:

JTableBinding<Employee, List<Employee>, JTable> bindings = SwingBindings.createJTableBinding( UpdateStrategy.READ_WRITE, EmployeeDatabase.getAll(), employeesTbl); Property<Employee, String> property = BeanProperty.create("description"); ColumnBinding col = bindings.addColumnBinding(property); col.setColumnName("Employee Names"); bindings.bind();

The preceding code binds a list of Employee POJOs (plain old Java objects) to a table, and shows one column (mapped to the description property) with an "Employee Names" column header. Similar methods exist in that class to bind to a JList or a JComboBox.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date