Class JListBinding<E,SS,TS>
- java.lang.Object
-
- org.jdesktop.beansbinding.Binding<SS,SV,TS,TV>
-
- org.jdesktop.beansbinding.AutoBinding<SS,List<E>,TS,List>
-
- org.jdesktop.swingbinding.JListBinding<E,SS,TS>
-
- Type Parameters:
E- the type of elements in the sourceListSS- the type of source object (on which the source property resolves toList)TS- the type of target object (on which the target property resolves toJList)
public final class JListBinding<E,SS,TS> extends AutoBinding<SS,List<E>,TS,List>
Binds aListof objects to act as the elements of aJList. Each object in the sourceListprovides one element in theJList. By setting aDetailBindingyou can specify the property to use to derive each list element from its corresponding object in the sourceList. The defaultDetailBindinguses the objects directly. Instances ofJListBindingare obtained by calling one of thecreateJListBindingmethods in theSwingBindingsclass.Here is an example of creating a binding from a
ListofPersonobjects to aJList:// create the person list Listpeople = createPersonList(); // create the binding from List to JList JListBinding lb = SwingBindings.createJListBinding(READ, people, jList); // define the property to be used to derive list elements ELProperty fullNameP = ELProperty.create("${firstName} ${lastName}"); // add the detail binding lb.setDetailBinding(fullNameP); // realize the binding lb.bind(); The
JListtarget of aJListBindingacts as a live view of the objects in the sourceList, regardless of the update strategy (the meaning of the update strategy is clarified later in this document).JListBindinglistens to the property specified for anyDetailBinding, for all objects in theList, and updates the values displayed in theJListin response to change. If theListis an instance ofObservableList, then changes to theListcontents (such as adding, removing or replacing an object) are also reflected in theJList. Important: Changing the contents of a non-observableListwhile it is participating in aJListBindingis unsupported, resulting in undefined behavior and possible exceptions.JListBindingrequires extra clarification on the operation of therefreshandsavemethods and the meaning of the update strategy. The target property of aJListBindingis not the targetJListproperty provided in the constructor, but rather a private synthetic property representing theListof objects to show in the targetJList. This synthetic property is readable/writeable only when theJListBindingis bound and the targetJListproperty is readable with anon-nullvalue.It is this private synthetic property on which the
refreshandsavemethods operate; meaning that these methods simply cause syncing between the value of the sourceListproperty and the value of the synthetic target property (representing theListto be shown in the targetJList). These methods do not, therefore, have anything to do with refreshing values in theJList. Likewise, the update strategy, which simply controls whenrefreshandsaveare automatically called, also has nothing to do with refreshing values in theJList.Note: At the current time, the
READ_WRITEupdate strategy is not useful forJListBinding. To prevent unwanted confusion,READ_WRITEis translated toREADbyJListBinding'sconstructor.JListBindingworks by installing a custom model on the targetJList, as appropriate, to represent the sourceList. The model is installed on a targetJListwith the first succesful call torefreshwith thatJListas the target. Subsequent calls torefreshupdate the elements in this already-installed model. The model is uninstalled from a targetJListwhen either theJListBindingis unbound or when the targetJListproperty changes to no longer represent thatJList. Note: When the model is uninstalled from aJList, it is replaced with aDefaultListModel, in order to leave theJListfunctional.Some of the above is easier to understand with an example. Let's consider a
JListBinding(binding), with update strategyREAD, between a property representing aList(listP) and a property representing aJList(jListP).listPandjListPboth start off readable, referring to anon-nullListandnon-nullJListrespectively. Let's look at what happens for each of a sequence of events:Sequence Event Result 1 explicit call to binding.bind()- synthetic target property becomes readable/writeable
-refresh()is called
- model is installed on targetJList, representing list of objects2 listPchanges to a newList- refresh()is called
- model is updated with new list of objects3 jListPchanges to a newJList- model is uninstalled from old JList4 explicit call to binding.refresh()- model is installed on target JList, representing list of objects5 listPchanges to a newList- refresh()is called
- model is updated with new list of objects6 explicit call to binding.unbind()- model is uninstalled from target JListNotice that in step 3, when the value of the
JListproperty changed, the newJListdid not automatically get the model with the elements applied to it. A change to the target value should not cause anAutoBindingto sync the target from the source. Step 4 forces a sync by explicitly callingrefresh. Alternatively, it could be caused by any other action that results in arefresh(for example, the source property changing value, or an explicit call tounbindfollowed bybind).DetailBindingsare managed by theJList. They are not to be explicitly bound, unbound, added to aBindingGroup, or accessed in a way that is not allowed for a managed binding.In addition to binding the elements of a
JList, it is possible to bind to the selection of aJList. When binding to the selection of aJListbacked by aJListBinding, the selection is always in terms of elements from the sourceList, regardless of anyDetailBindingspecified. See the list of interesting swing properties in the package summary for more details.- Author:
- Shannon Hickey
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classJListBinding.DetailBindingDetailBindingrepresents a binding between a property of the elements in theJListBinding'ssourceList, and the values shown in theJList.-
Nested classes/interfaces inherited from class org.jdesktop.beansbinding.AutoBinding
AutoBinding.UpdateStrategy
-
Nested classes/interfaces inherited from class org.jdesktop.beansbinding.Binding
Binding.CST, Binding.SyncFailure, Binding.SyncFailureType, Binding.ValueResult<V>
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedJListBinding(AutoBinding.UpdateStrategy strategy, SS sourceObject, Property<SS,List<E>> sourceListProperty, TS targetObject, Property<TS,? extends JList> targetJListProperty, String name)Constructs an instance ofJListBinding.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidbindImpl()Called byBinding.bind()to allow subclasses to initiate binding.JListBinding.DetailBindinggetDetailBinding()Returns theDetailBindingfor thisJListBinding.JListBinding.DetailBindingsetDetailBinding(Property<E,?> detailProperty)Creates aDetailBindingand sets it as theDetailBindingfor thisJListBinding.JListBinding.DetailBindingsetDetailBinding(Property<E,?> detailProperty, String name)Creates a namedDetailBindingand sets it as theDetailBindingfor thisJListBinding.protected voidunbindImpl()Called byBinding.unbind()to allow subclasses to uninitiate binding.-
Methods inherited from class org.jdesktop.beansbinding.AutoBinding
getUpdateStrategy, paramString, sourceChangedImpl, targetChangedImpl
-
Methods inherited from class org.jdesktop.beansbinding.Binding
addBindingListener, addPropertyChangeListener, addPropertyChangeListener, bind, bindUnmanaged, firePropertyChange, getBindingListeners, getConverter, getName, getPropertyChangeListeners, getPropertyChangeListeners, getSourceNullValue, getSourceObject, getSourceProperty, getSourceUnreadableValue, getSourceValueForTarget, getTargetNullValue, getTargetObject, getTargetProperty, getTargetValueForSource, getValidator, isBound, isManaged, isSourceUnreadableValueSet, notifySynced, notifySyncFailed, notifySyncWarning, refresh, refreshAndNotify, refreshAndNotifyUnmanaged, refreshUnmanaged, removeBindingListener, removePropertyChangeListener, removePropertyChangeListener, save, saveAndNotify, saveAndNotifyUnmanaged, saveUnmanaged, setConverter, setManaged, setSourceNullValue, setSourceObject, setSourceObjectUnmanaged, setSourceProperty, setSourceUnreadableValue, setTargetNullValue, setTargetObject, setTargetObjectUnmanaged, setTargetProperty, setValidator, throwIfBound, throwIfManaged, throwIfUnbound, toString, unbind, unbindUnmanaged, unsetSourceUnreadableValue
-
-
-
-
Constructor Detail
-
JListBinding
protected JListBinding(AutoBinding.UpdateStrategy strategy, SS sourceObject, Property<SS,List<E>> sourceListProperty, TS targetObject, Property<TS,? extends JList> targetJListProperty, String name)
Constructs an instance ofJListBinding.- Parameters:
strategy- the update strategysourceObject- the source objectsourceListProperty- a property on the source object that resolves to theListof elementstargetObject- the target objecttargetJListProperty- a property on the target object that resolves to aJListname- a name for theJListBinding- Throws:
IllegalArgumentException- if the source property or target property isnull
-
-
Method Detail
-
bindImpl
protected void bindImpl()
Description copied from class:BindingCalled byBinding.bind()to allow subclasses to initiate binding. Subclasses typically need not installPropertyStateListenerson the source property and target property as they will be notified by calls toBinding.sourceChangedImpl(org.jdesktop.beansbinding.PropertyStateEvent)andBinding.targetChangedImpl(org.jdesktop.beansbinding.PropertyStateEvent)when the source and target properties change respectively.- Overrides:
bindImplin classAutoBinding<SS,List<E>,TS,List>- See Also:
Binding.unbindImpl()
-
unbindImpl
protected void unbindImpl()
Description copied from class:BindingCalled byBinding.unbind()to allow subclasses to uninitiate binding.- Overrides:
unbindImplin classAutoBinding<SS,List<E>,TS,List>- See Also:
Binding.bindImpl()
-
setDetailBinding
public JListBinding.DetailBinding setDetailBinding(Property<E,?> detailProperty)
Creates aDetailBindingand sets it as theDetailBindingfor thisJListBinding. ADetailBindingspecifies the property of the objects in the sourceListto be used as the elements of theJList. If thedetailPropertyparameter isnull, theDetailBindingspecifies that the objects themselves be used.- Parameters:
detailProperty- the property with which to derive each list value from its corresponding object in the sourceList- Returns:
- the
DetailBinding
-
setDetailBinding
public JListBinding.DetailBinding setDetailBinding(Property<E,?> detailProperty, String name)
Creates a namedDetailBindingand sets it as theDetailBindingfor thisJListBinding. ADetailBindingspecifies the property of the objects in the sourceListto be used as the elements of theJList. If thedetailPropertyparameter isnull, theDetailBindingspecifies that the objects themselves be used.- Parameters:
detailProperty- the property with which to derive each list value from its corresponding object in the sourceList- Returns:
- the
DetailBinding
-
getDetailBinding
public JListBinding.DetailBinding getDetailBinding()
Returns theDetailBindingfor thisJListBinding. ADetailBindingspecifies the property of the sourceListelements to be used as the elements of theJList.- Returns:
- the
DetailBinding - See Also:
setDetailBinding(Property, String)
-
-