JForm is a Swing component framework that supports databinding and validation.



JForm is a Swing component framework that supports databinding and
validation.

JForm was first released in september 2003 as opensource project on
dev.java.net.
I tried to collaborate with JDNC, JGoodies and Spring RCP but no one
was
interested so I decided to continue development and use JForm in
projects
for my customers. Over the past years it has evolved into a production
stable
full featured Swing component framework.

DataBinding with JForm is easy, With JForm's generic FormModel it's
easy
to bind to any kind of datasource like JavaBeans, ResultSet and
Collections.
Here's an example of binding a field to a JavaBean property.
Suppose you have a bean like this and want to bind price:

public class Product {
private long int;
private String description;
private double price;

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}
}

Only one line of code does the job: InputField price = new
InputField("product.price");

Well I can't make it any simpler than this. Notice the Product class
does not
subclass some super class or implement some interface so you can use
your
existing beans just the way they are.

Okay how does binding work when you have a list with products where a
user can
select a product from the list. Again really easy (and still one line):

SelectField product = new SelectField("products", "product");

You don't have to worry about creating a model, JForm takes care of
that!
Ofcourse it's still possible to use your own custom model if you need
to.
And JForm automatically selects the product in the list for you.

Back to our price field, the user entered/modified the price, JForm
automatically
validates the field value. When all is ok you get an onChange event.
When the value is invalid an onError (ValidationError) is thrown and
default
focus is set to the field and the backgroundcolor is set to red to
indicate an error.
Ofcourse JForm cannot automatically validate every kind of type but
don't panic, you
can write your own custom validators. Each field can have as many
validators as
needed and (the same) validators are reusable for other fields as well.

Another important requirement for forms is navigation better know as
the 'tab index'.
On a FormPage you can set the field that should get focus immediatly
after the form is
refreshed with setInitialFocusField(firstHourField). There are also
methods to set
the first and last focus field on a page so you can make nice focus
'loops'.
And for the real die hards you can hardcode the tab index for each
individual field.

As a developer you want to know what's happening on a form, normally
with Swing you
have to add all kinds of listeners (action, focus, mouse, etc) to get
events.
With JForm all you have to do is set one field event handler and you
only need to listen
for the type of event(s) you're interested in. Event handling with
JForm is much like
JavaScript wich I personally find much easier. Currently supported
events are:
onFocus, onClick, onChange (value changed or selection changed) and
onError.

The people who designed Swing thought it would be nice to have a
different model for
comboboxes, lists and tables. We know better! JForm comes with a
DataModel that can be
used for combox, list and table. Write one model and use it everywhere
(isn't this what
Java is all about?). JForm's ComboField, ListField and TableField all
have standard
support for sorting and filtering (and more in the future). Ofcourse
the currently selected
value stays selected when sorting order has changed and/or a filter has
changed.

Talking about comboboxes, lists and tables... renderers!
For list you need a ListCellRenderer and for table you need a
TableCellRenderer...
I don't know about you but I often find myself writing the same
renderer for lists and tables.
And whatif I want to use more than one renderer on the same list or
table?
I have reimplemented renderers so you can write a renderer that can be
used for all components
that support a renderer.

I have not discussed all features of JForm here, once you have used
it's full potential you
discover writing (form-based) Swing applications can be real fun and
safe you a lot of time!

One more thing, as I understand developers find it hard to use
threading with Swing or even how
to start writing Swing applications. This year I have developed an
application framework for Swing
called SwingForms. SwingForms is a model-view-controller architecture
that makes (multi)threading
transparent and forces developers to write well structered Swing
applications.

For more information about JForm and SwingForms visit my website
http://www.coderight.nl

I wish everyone a happy new year,
Will - CodeRight

.