Skip to main content

The Missing Link

Posted by mkarg on January 3, 2010 at 6:47 AM PST

Attaching a GUI to a domain model object (a.k.a. "Entity") is a boring job. You need to write lots of synchronization code or models to change the UI when the entity changes and vice versa. Now that has an end. Here is the ultimate, automatic glue generator: The Java Beans Binding API. It allows you to glue together two Java Beans (i. e. POJOs, and has nothing to do with Swing or EJB). Just tell the API that you want to glue property X of beans A to property Y of bean B and "magically" from now on they will have always the same value. It even will internally translate data types! Cool stuff. What can we do with it? For example, you could tell the API that you want to glue your GUI directly to a EJB 3 Entity. How easier could it be to code a data view? Here is a small sample. Actually it is not using EJB 3, but hey, as you know, turning that class Person in a EJB 3 entity is as easy as adding @Entity to it's definition... ;-)

 public final class Person {

private String firstName;

private final String lastName;

public Person(final String firstName, final String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public final String getFirstName() {
return this.firstName;
}

public final String getLastName() {
return this.lastName;
}

public final void setFirstName(final String firstName) {
this.firstName = firstName;

System.out.println("Somebody renamed this to: " + this.firstName);
}

}

 public final class SwingBindingSample {

public static final void main(final String[] arguments) {
final JTable jTable = new JTable();

final List<Person> persons = SwingBindingSample.createPersonList();

final JTableBinding jTableBinding = SwingBindings.createJTableBinding(UpdateStrategy.READ_WRITE, persons, jTable);

jTableBinding.addColumnBinding(BeanProperty.create("firstName"));
jTableBinding.addColumnBinding(BeanProperty.create("lastName"));

jTableBinding.bind();

final JFrame jFrame = new JFrame();
jFrame.add(jTable);
jFrame.pack();
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}

private static final List<Person> createPersonList() {
final List<Person> list = new LinkedList<Person>();
list.add(new Person("Markus", "Karg"));
list.add(new Person("Sprockey", "The Dog"));
return list;
}

}

Run it, and you will see a JFrame that shows a JTable. The JTable will show two lines showing the persons Markus Karg and Sprockey The Dog. Now comes the cool thing: The API detects automatically that there is no setLastName() method in the Person class. As a result, it prevents that the JTable switches to edit mode, while it still accepts that you press F2 to edit the first name (as a proof, the Person call uses System.out.println() to log that). Actually this sample uses a specialized form of the API that is aware of JTable. This is needed to support tricks like that.

Isn't that cool? Just one code line to link a property? Think of all the thousands of code lines that we can just throw away... :-)

Comments

Still missing: change notification

robust bean implementations must fire property change events in setters. Without the ui update is unreliable

Jeanette

 

Just a sample

You are right. We shoretened the code to make it easier to read in this blog posting.