Summary
Beans Binding (JSR 295) is a new JCP standard for binding two bean properties, especially in UI components. In a recent pair of tutorials, Geertjan Wielenga explains with two short examples how to create a master-detail UI with Beans Binding and Swing.
Advertisement
Most tutorials on Beans Binding (JSR 295) have, up to now, focused either on the minute details of this new API, or demonstrated how NetBeans could automate much of tedium of binding user interface properties to business objects and vice versa. As a result, much example code available for Beans Binding is either specific to NetBeans and its GUI editor, or confined to small snippets illustrating some detail of the JSR 295 API.
NetBeans evangelist Geertjan Wielenga has recently broke that mold, and created two short, to-the-point examples that don't rely on automatic code generation in NetBeans, but that also provide a complete overview of a master-view UI with Swing and Beans Binding, in Beans Binding Via The Road Less Travelled By:
At the end of this blog entry, you'll have a JFrame that contains a JTable. The JTable is filled with data from a database. Whenever you select a row in the JTable, the JTextFields below the JTable are filled with the data from the currently selected row...
In the explanation that follows, we will ONLY use the code editor. Nothing will be generated. And, again, I could have used a Swing Application Framework template to generate all of the above (plus a lot more, such as CRUD functionality)...
In the second example, Wielenga shows how to create a similar UI using a JList.
The examples use client-side JPA data access, requiring an entity object. Once the data access part of the application is set up, Wielenga focuses on binding properties of the JTable to properties of the business entity, and then creating text field components that are, in turn, bound to the selected element in the table:
Note the pattern by which things happen: we create a binding group, we add other binding groups to it, within which we add bindings. The bindings use expression language to link to database column names, they also specify table column names and the type of the data...
I know it's just an example but the bean is pretty extraneous. I mean, why should I have to code up a Customer class just so I use it like a map or dict?
It just seems like a lot of complexity all in order to hardcode the structure of the database table in Java.
> I know it's just an example but the bean is pretty > extraneous. I mean, why should I have to code up a > Customer class just so I use it like a map or dict? > > It just seems like a lot of complexity all in order to > hardcode the structure of the database table in Java.
Well, the bean in the example is more like a messenger object used to ferry data between the database and the UI. As the article notes, these sorts of objects can be generated automatically from a database schema by NetBeans.
> > I know it's just an example but the bean is pretty > > extraneous. I mean, why should I have to code up a > > Customer class just so I use it like a map or dict? > > > > It just seems like a lot of complexity all in order to > > hardcode the structure of the database table in Java. > > Well, the bean in the example is more like a messenger > object used to ferry data between the database and the UI.
Precisely my point. The bean class is extraneous.
> As the article notes, these sorts of objects can be > generated automatically from a database schema by NetBeans.
Being able to generate pointless code doesn't make it less pointless. And it doesn't change the fact that the classes need to be created.
It's one thing to create domain objects with behaviors and bind them to a database and UI but they way most beans are used is as a glorified working storage area.