Skip to main content

Beans Binding 1.0 Released

Posted by shan_man on September 5, 2007 at 4:53 PM PDT

-- From Saint Petersburg, Russia @ 3:30 AM local time

It's finally time! After months of eating, sleeping and drinking
Beans Binding, I'm thrilled to finally announce that version 1.0 has been released at
http://beansbinding.dev.java.net
for your binding pleasure!

1.0 represents a major re-architecture of the Beans Binding API, based on the
extremely valuable feedback from members of the expert group and the community.
I'd like to say thank you to all of you who have discussed ideas with me and
contributed your own. I've tried to combine and build on these ideas and create
an API that is powerful, flexible, extensible and easy to use. I hope you'll keep
the feedback coming so we can continue this rewarding process.

While the work on this project continues through the JCP process,
with feature additions and possible API changes to come, the current
state represents the core API that is expected, for the most part, to
persist. As such, we're calling it 1.0 and releasing!

Some of the major points of interest in this release:

  • The concept of a property has been factored out into an abstract Property class,
    with two concrete implementations of interest: BeanProperty and ELProperty.

  • Binding is now an abstract class representing a binding between two Property
    instances (typically associated with two objects).

  • Binding with automatic syncing is implemented by a new concrete AutoBinding subclass.
  • Bindings to complex Swing components (such as JTable, JList and JComboBox) are
    now handled by custom Binding subclasses.

  • The synthetic Swing properties that offer multiple possible behaviors are now exposed
    via multiple versions of the property.
    For example: "text", "text_ON_FOCUS_LOST" and "text_ON_ACTION_OR_FOCUS_LOST"
    for JTextField; "selectedElement" and "selectedElement_IGNORE_ADJUSTING" for
    JList and JTable.

  • Everything has been repackaged into org.jdesktop packages.

It is my hope that the JavaDoc provided with this release will have you well on your way to binding with the new API
very quickly. While I would love to take you on a tour through the entire API here, composing such a tutorial would delay
this announcement. As such, I'll present a few examples to get you started, let you get going with the new API, and then follow up
with additional blog entries, as time allows, on particulars deserving more attention. As always, please feel free to send
me your questions and/or feedback at any time.

Examples

For the purposes of these examples, we'll work on Person beans, and assume that Person
has the following properties: firstName, lastName, age and mother.

Creating and using BeanProperty and ELProperty:



  // create a BeanProperty representing a bean's firstName
  Property firstP = BeanProperty.create("firstName");

  // create a BeanProperty representing a bean's lastName
  Property lastP = BeanProperty.create("lastName");

  // create a BeanProperty representing the firstName of
  // a bean's mother
  Property motherFirstP = BeanProperty.create("mother.firstName");

  // creata an ELProperty representing a bean's mother's full name:
  Property motherFullP =
      ELProperty.create("${mother.firstName} ${mother.lastName}");

  // create an ELProperty representing whether or not a bean's
  // mother's age is greater than 65
  Property motherOlderP = ELProperty.create("${mother.age > 65}");


  // print Duke's first name
  System.out.println(firstP.getValue(duke));

  // print John's first name, reusing the same property
  System.out.println(firstP.getValue(john));

  // print Duke's mother's full name
  System.out.println(motherFullP.getValue(duke));

  // set Duke's mother's first name
  motherFirstP.setValue(duke, "Jennifer");

  // set John's mother's first name, reusing the same property
  motherFirstP.setValue(john, "Melanie");

  // listen for changes to motherOlderP for Duke
  motherOlderP.addPropertyStateListener(duke, listener);

Creating AutoBindings between two properties:



  // Bind Duke's first name to the text property of a Swing JTextField
  BeanProperty textP = BeanProperty.create("text");
  Binding binding =
      Bindings.createAutoBinding(READ_WRITE, duke, firstP, textfield, textP);
  binding.bind();

  // Bind Duke's mother's first name to the text property of a Swing JTextField,
  // specifying that the JTextField's text property only reports change
  // (thereby updating the source of the READ_WRITE binding) on focus lost
  BeanProperty textP = BeanProperty.create("text_ON_FOCUS_LOST");
  Binding binding =
      Bindings.createAutoBinding(READ_WRITE, duke, motherFirstP, textfield, textP);
  binding.bind();

  // Bind the motherOlderP for Duke to the selection of a display-only Swing JCheckBox
  BeanProperty selectedP = BeanProperty.create("selected");
  Binding binding =
      Bindings.createAutoBinding(READ, duke, motherOlderP, checkBox, selectedP);
  binding.bind();

Using Swing Binding subclasses:



  // Bind a List of Person objects as the elements of a JTable and specify
  // that columns should be shown for the beans' first name, mother's full
  // name and whether or not the mother is older than 65
  JTableBinding tb = SwingBindings.createJTableBinding(READ, personList, personJTable);
  tb.addColumnBinding(firstP)
      .setColumnName("First Name").setColumnClass(String.class);
  tb.addColumnBinding(motherFullP)
      .setColumnName("Mother's Full Name").setColumnClass(String.class);
  tb.addColumnBinding(motherOlderP)
      .setColumnName("Mother older than 65").setColumnClass(Boolean.class);
  tb.bind();

  // Bind the last name of the table's selected element to a JTextField
  Property selectedLastP = BeanProperty.create("selectedElement.lastName");
  Property textP = BeanProperty.create("text");
  Binding selBinding =
      Bindings.createAutoBinding(READ_WRITE, personJTable, selectedLastP,
                                             textfield, textP);
  selBinding.bind();

  // Bind a List of Person objects as the elements of a JList
  // and specify that the list should display the first name of the beans
  JListBinding lb = SwingBindings.createJListBinding(personList, personJList);
  lb.setDetailBinding(firstP);

  // Bind the last name of the list's selected element to a JTextField,
  // specifying that changes to the selected element are ignored while
  // it is adjusting
  Property selectedLastP =
      BeanProperty.create("selectedElement_IGNORE_ADJUSTING.lastName");
  Property textP = BeanProperty.create("text");
  Binding selBinding =
      Bindings.createAutoBinding(READ_WRITE, personJList, selectedLastP,
                                             textfield, textP);
  selBinding.bind();

Related Topics >>

Comments

 Hi, im having a trouble with this code, Im trying to ...

Hi, im having a trouble with this code, Im trying to run it, but in this line:

// print Duke's mother's full name

[prettify]  System.out.println(motherFullP.getValue(duke));  Im getting an exception. It seems like it doesn&#039;t accept &quot;mother.firstName&quot; when creating the property the exception says: UnsopportedOperationException: Unredable. <br type="_moz" />[/prettify]
[prettify]Thanks.  &lt;br /&gt;&lt;br /&gt;&lt;br type=&quot;_moz&quot; /&gt;[/prettify]