Class JTableBinding<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.JTableBinding<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 toJTable)
public final class JTableBinding<E,SS,TS> extends AutoBinding<SS,List<E>,TS,List>
Binds aListof objects to act as the rows of aJTable. Each object in the sourceListrepresents one row in theJTable. Mappings from properties of the source objects to columns are created by addingColumnBindingsto aJTableBinding. Instances ofJTableBindingare obtained by calling one of thecreateJTableBindingmethods in theSwingBindingsclass.Here is an example of creating a binding from a
ListofPersonobjects to aJTable:// create the person List Listpeople = createPersonList(); // create the binding from List to JTable JTableBinding tb = SwingBindings.createJTableBinding(READ, people, jTable); // define the properties to be used for the columns BeanProperty firstNameP = BeanProperty.create("firstName"); BeanProperty lastNameP = BeanProperty.create("lastName"); BeanProperty ageP = BeanProperty.create("age"); // configure how the properties map to columns tb.addColumnBinding(firstNameP).setColumnName("First Name"); tb.addColumnBinding(lastNameP).setColumnName("Last Name"); tb.addColumnBinding(ageP).setColumnName("Age").setColumnClass(Integer.class); // realize the binding tb.bind(); The
JTabletarget of aJTableBindingacts 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).JTableBindinglistens to the properties specified for theColumnBindings, for all objects in theList, and updates the values displayed in theJTablein response to change. All successful edits made toJTablecell values are immediately committed back to corresponding objects in the sourceList. If theListis an instance ofObservableList, then changes to theListcontents (such as adding, removing or replacing an object) are also reflected in theJTable. Important: Changing the contents of a non-observableListwhile it is participating in aJTableBindingis unsupported, resulting in undefined behavior and possible exceptions.A cell in the
JTableis editable for any given row and column when all of the following are true: the property specified for that column by itsColumnBindingis writeable for the object representing that row, the"editable"property of theJTableBindingistrue(the default), and the"editable"property of theColumnBindingistrue(the default).JTableBindingrequires extra clarification on the operation of therefreshandsavemethods and the meaning of the update strategy. The target property of aJTableBindingis not the targetJTableproperty provided in the constructor, but rather a private synthetic property representing theListof objects to show in the targetJTable. This synthetic property is readable/writeable only when theJTableBindingis bound and the targetJTableproperty 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 targetJTable). These methods do not, therefore, have anything to do with refreshing or saving values in theJTable. Likewise, the update strategy, which simply controls whenrefreshandsaveare automatically called, also has nothing to do with refreshing or saving values in theJTable.Note: At the current time, the
READ_WRITEupdate strategy is not useful forJTableBinding. To prevent unwanted confusion,READ_WRITEis translated toREADbyJTableBinding'sconstructor.JTableBindingworks by installing a custom model on the targetJTable, as appropriate, to represent the sourceList. The model is installed on a targetJTablewith the first succesful call torefreshwith thatJTableas the target. Subsequent calls torefreshupdate the elements in this already-installed model. The model is uninstalled from a targetJTablewhen either theJTableBindingis unbound or when the targetJTableproperty changes to no longer represent thatJTable. Note: When the model is uninstalled from aJTable, it is replaced with aDefaultTableModel, in order to leave theJTablefunctional.Some of the above is easier to understand with an example. Let's consider a
JTableBinding(binding), with update strategyREAD, between a property representing aList(listP) and a property representing aJTable(jTableP).listPandjTablePboth start off readable, referring to anon-nullListandnon-nullJTablerespectively. 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 targetJTable, representing list of objects2 listPchanges to a newList- refresh()is called
- model is updated with new list of objects3 jTablePchanges to a newJTable- model is uninstalled from old JTable4 explicit call to binding.refresh()- model is installed on target JTable, 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 JTableNotice that in step 3, when the value of the
JTableproperty changed, the newJTabledid 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).ColumnBindingsare managed by theJTableBinding. They are not to be explicitly bound, unbound, added to aBindingGroup, or accessed in a way that is not allowed for a managed binding.BindingListenersadded to aColumnBindingare notified at the time an editedJTablevalue is to be committed back to the sourceList. They receive notification of eithersyncedorsyncFailed.BindingListenersadded to theJTableBindingitself are also notified ofsyncandsyncFailedfor theJTableBinding's ColumnBindings.In addition to binding the elements of a
JTable, it is possible to bind to the selection of aJTable. When binding to the selection of aJTablebacked by aJTableBinding, the selection is always in terms of elements from the sourceList. 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 classJTableBinding.ColumnBindingColumnBindingrepresents a binding between a property of the elements in theJTableBinding'ssourceList, and a column in the table.-
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 protectedJTableBinding(AutoBinding.UpdateStrategy strategy, SS sourceObject, Property<SS,List<E>> sourceListProperty, TS targetObject, Property<TS,? extends JTable> targetJTableProperty, String name)Constructs an instance ofJTableBinding.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JTableBinding.ColumnBindingaddColumnBinding(int index, Property<E,?> columnProperty)Creates aColumnBindingand inserts it at the given index into the list ofColumnBindingsmaintained by thisJTableBinding.JTableBinding.ColumnBindingaddColumnBinding(int index, Property<E,?> columnProperty, String name)Creates aColumnBindingand inserts it at the given index into the list ofColumnBindingsmaintained by thisJTableBinding.JTableBinding.ColumnBindingaddColumnBinding(Property<E,?> columnProperty)Creates aColumnBindingand adds it to the end of the list ofColumnBindingsmaintained by thisJTableBinding.JTableBinding.ColumnBindingaddColumnBinding(Property<E,?> columnProperty, String name)Creates a namedColumnBindingand adds it to the end of the list ofColumnBindingsmaintained by thisJTableBinding.protected voidbindImpl()Called byBinding.bind()to allow subclasses to initiate binding.JTableBinding.ColumnBindinggetColumnBinding(int index)Returns theColumnBindingwith the given index in the list maintained by thisJTableBinding.List<JTableBinding.ColumnBinding>getColumnBindings()Returns an unmodifiable copy of the list ofColumnBindingsmaintained by thisJTableBinding.booleanisEditable()Returns whether or not the cells of the table should be editable.JTableBinding.ColumnBindingremoveColumnBinding(int index)Removes theColumnBindingwith the given index from the list maintained by thisJTableBinding.booleanremoveColumnBinding(JTableBinding.ColumnBinding binding)Removes the givenColumnBindingfrom the list maintained by thisJTableBinding.voidsetEditable(boolean editable)Sets whether or not the cells of the table should be editable.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
-
JTableBinding
protected JTableBinding(AutoBinding.UpdateStrategy strategy, SS sourceObject, Property<SS,List<E>> sourceListProperty, TS targetObject, Property<TS,? extends JTable> targetJTableProperty, String name)
Constructs an instance ofJTableBinding.- Parameters:
strategy- the update strategysourceObject- the source objectsourceListProperty- a property on the source object that resolves to theListof elementstargetObject- the target objecttargetJTableProperty- a property on the target object that resolves to aJTablename- a name for theJTableBinding- 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()
-
setEditable
public void setEditable(boolean editable)
Sets whether or not the cells of the table should be editable. The default for this property istrue. See this paragraph in the class level documentation on editability.- Parameters:
editable- whether or not the cells of the table should be editable
-
isEditable
public boolean isEditable()
Returns whether or not the cells of the table should be editable. The default for this property istrue. See this paragraph in the class level documentation on editability.- Returns:
- whether or not the cells of the table should be editable
-
addColumnBinding
public JTableBinding.ColumnBinding addColumnBinding(Property<E,?> columnProperty)
Creates aColumnBindingand adds it to the end of the list ofColumnBindingsmaintained by thisJTableBinding.The list of
ColumnBindingsdictates the columns to be displayed in theJTable, with aColumnBinding'sorder in the list determining its table model index.- Parameters:
columnProperty- the property with which to derive cell values from the elements of the sourceList- Returns:
- the
ColumnBinding - Throws:
IllegalArgumentException- ifcolumnPropertyisnull- See Also:
JTableBinding.ColumnBinding
-
addColumnBinding
public JTableBinding.ColumnBinding addColumnBinding(Property<E,?> columnProperty, String name)
Creates a namedColumnBindingand adds it to the end of the list ofColumnBindingsmaintained by thisJTableBinding.The list of
ColumnBindingsdictates the columns to be displayed in theJTable, with aColumnBinding'sorder in the list determining its table model index.- Parameters:
columnProperty- the property with which to derive cell values from the elements of the sourceListname- a name for the column binding- Returns:
- the
ColumnBinding - Throws:
IllegalArgumentException- ifcolumnPropertyisnull- See Also:
JTableBinding.ColumnBinding
-
addColumnBinding
public JTableBinding.ColumnBinding addColumnBinding(int index, Property<E,?> columnProperty)
Creates aColumnBindingand inserts it at the given index into the list ofColumnBindingsmaintained by thisJTableBinding.The list of
ColumnBindingsdictates the columns to be displayed in theJTable, with aColumnBinding'sorder in the list determining its table model index.- Parameters:
index- the index at which to insert theColumnBindingcolumnProperty- the property with which to derive cell values from the elements of the sourceList- Returns:
- the
ColumnBinding - Throws:
IllegalArgumentException- ifcolumnPropertyisnull- See Also:
JTableBinding.ColumnBinding
-
addColumnBinding
public JTableBinding.ColumnBinding addColumnBinding(int index, Property<E,?> columnProperty, String name)
Creates aColumnBindingand inserts it at the given index into the list ofColumnBindingsmaintained by thisJTableBinding.The list of
ColumnBindingsdictates the columns to be displayed in theJTable, with aColumnBinding'sorder in the list determining its table model index.- Parameters:
index- the index at which to insert theColumnBindingcolumnProperty- the property with which to derive cell values from the elements of the sourceListname- a name for theColumnBinding- Returns:
- the
ColumnBinding - Throws:
IllegalArgumentException- ifcolumnPropertyisnull- See Also:
JTableBinding.ColumnBinding
-
removeColumnBinding
public boolean removeColumnBinding(JTableBinding.ColumnBinding binding)
Removes the givenColumnBindingfrom the list maintained by thisJTableBinding.The list of
ColumnBindingsdictates the columns to be displayed in theJTable, with aColumnBinding'sorder in the list determining its table model index.- Parameters:
binding- theColumnBindingto remove- See Also:
addColumnBinding(Property, String)
-
removeColumnBinding
public JTableBinding.ColumnBinding removeColumnBinding(int index)
Removes theColumnBindingwith the given index from the list maintained by thisJTableBinding.The list of
ColumnBindingsdictates the columns to be displayed in theJTable, with aColumnBinding'sorder in the list determining its table model index.- Parameters:
index- the index of theColumnBindingto remove- See Also:
addColumnBinding(Property, String)
-
getColumnBinding
public JTableBinding.ColumnBinding getColumnBinding(int index)
Returns theColumnBindingwith the given index in the list maintained by thisJTableBinding.The list of
ColumnBindingsdictates the columns to be displayed in theJTable, with aColumnBinding'sorder in the list determining its table model index.- Parameters:
index- the index of theColumnBindingto return- Returns:
- the
ColumnBindingat the given index - See Also:
addColumnBinding(Property, String)
-
getColumnBindings
public List<JTableBinding.ColumnBinding> getColumnBindings()
Returns an unmodifiable copy of the list ofColumnBindingsmaintained by thisJTableBinding.The list of
ColumnBindingsdictates the columns to be displayed in theJTable, with aColumnBinding'sorder in the list determining its table model index.- Returns:
- the list of
ColumnBindings - See Also:
addColumnBinding(Property, String)
-
-