Holding properties in Items
Examples on the use of items include rows in a Table, with the properties corresponding to table columns, nodes in a Tree, and the the data bound to a Form, with item’s properties bound to individual form fields.
Items are generally equivalent to objects in the object-oriented model, but with the exception that they are configurable and provide an event handling mechanism. The simplest way to utilize Item interface is to use existing implementations. Provided utility classes include a configurable property set ( PropertysetItem) and a bean-to-item adapter ( BeanItem). Also, a Form implements the interface and can therefore be used directly as an item.
In addition to being used indirectly by many user interface components, items provide the basic data model underlying the Form component. In simple cases, forms can even be generated automatically from items. The properties of the item correspond to the fields of the form.
The Item interface defines inner interfaces for maintaining the item property set and listening changes made to it. PropertySetChangeEvent events can be emitted by a class implementing the PropertySetChangeNotifier interface. They can be received through the PropertySetChangeListener interface.
The following example demonstrates a typical case of collecting ObjectProperty properties in an item:
Wrapping a Bean in a BeanItem
The BeanItem implementation of the Item interface is a wrapper for Java Bean objects. In fact, only the setters and getters are required while serialization and other bean features are not, so you can wrap almost any POJOs with minimal requirements.
You can use the getBean() method to get a reference to the underlying bean.
You may often have composite classes where one class “has a” another class. For example, consider the following Planet class which “has a” discoverer:
The difference is that NestedMethodProperty does not access the nested bean immediately but only when accessing the property values, while when using MethodProperty the nested bean is accessed when creating the method property. The difference is only significant if the nested bean can be null or be changed later.
You can use such a bean item for example in a Form as follows:
A Form with Nested Bean Properties