javax.swing.binding
Class SwingBindingSupport

java.lang.Object
  extended by javax.swing.binding.SwingBindingSupport

public final class SwingBindingSupport
extends java.lang.Object

SwingBindingSupport provides constants and documentation for binding to Swing components. The following describes how to bind to Swing components.

JCheckBox

The primary property to bind is the "selected" property. The following illustrates creating a binding targetting a JCheckBox and specifying it should not disable if the source path is incomplete:
   Binding binding = new Binding(source, "sourcePath", jCheckBox, "selected",
       SwingBindingSupport.DisableOnIncompletePathParameter, false);
 

JComboBox

Binding to a JComboBox is a two phase process. You first bind to the "elements" property. The "elements" property specifies the contents of the JComboBox:
   List bugTypes;
   // Create the binding specifying the 
   Binding binding = new Binding(bugTypes, null, jComboBox, "elements");
   // Specify that each element is to be shown using the 'description'
   // property. If this is not specified, the elements in the list are
   // used used directly.
   binding.addBinding("description", null);
 
The next step is to bind to the selection. There are two ways to do this. The first is to bind to an element of this list. This is useful if the object controlling the selection property is of the same type as that of the elements of the JComboBox:
   Binding binding = new Binding(selection, "bugType", 
                                 jComboBox, "selectedElement");
   // Specify that each element is to be shown using the 'description'
   // property. If this is not specified, the elements in the list are
   // used used directly.
   binding.addBinding("description", null);
 
The second option is to bind the selection to a property of the elements in the list. This involves binding to the "selectedElementProperty" and specifying the expression to apply to each element when binding to the elements. The following example illustrates this:
   List bugTypes;
   // Create the binding specifying the 
   Binding binding = new Binding(bugTypes, null, jComboBox, "elements",
   // Specify that the "selectedElementProperty" corresponds to
   // selectedObject.id.
           SwingBindingSupport.ComboBoxSelectedObjectPropertyParameter, "${id}");

   // Bind to the selection
   binding = new Binding(source, "id", jComboBox, "selectedElementProperty");
 

JList

The primary property to bind is the "elements" property. Binding to the "elements" property results in creating and setting a custom ListModel on the target JList. The source property must resolve to a List. If the source property resolves to an ObservableList, the ListModel tracks changes as appropriate. A specific property can be specified for each element using a child Binding. The following illustrates creating a binding targetting a JList. The value for each element is obtained using the 'firstName' property of each Customer.
   ObservableList customers;
   // Create the binding for the List.
   Binding binding = new Binding(customers, null, jList, "elements");
   // Specify getValueAt is to return the 'firstName' property of
   // each element.
   binding.addBinding("firstName", null);
 
The property delegate for JList also provides support for the "selectedElement" and "selectedElements" properties. The "selectedElement" property corresponds to the selected element (in terms of an element of the List bound to the "elements" property). The "selectedElements" property is a List of the selected elements. Both values change as the selection of the JList is modified.

JSlider

The primary property to bind is the "value" property. The following illustrates creating a binding targetting a JSlider. As disable on incomplete path has not been specified, the slider disables if the source path is incomplete.
   Binding binding = new Binding(source, "sourcePath", jSlider, "value");
 

JTable

JTable provides similar properties to that of JList; the "elements" properties, of type List, specifies the contents of the JTable (or more correctly, the TableModel), and the "selectedElement" and "selectedElements" properties may be used to track changes in the selection. When binding to a JTable, you must specify how the value for each column is obtained. This is done using the binding property TableColumnParameter. The following illustrates creating a binding targetting a JTable. Two columns are created, the first using the property "firstName", and the second "lastName".
   ObservableList customers;
   // Create the binding for the List.
   Binding binding = new Binding(customers, null, jTable, "elements");
   // Specify the first column should use the "firstName" property
   binding.addBinding("firstName", null, 
                      SwingBindingSupport.TableColumnParameter, 0);
   // Specify the second column should use the "lastName" property
   binding.addBinding("lastName", null, 
                      SwingBindingSupport.TableColumnParameter, 1);
 

JTextComponent

The primary property to bind to is the "text" property. By default, the "text" property is updated when enter is pressed, or focus is lost. This may be changed using the binding property TextChangeStrategyParameter. The following illustrates creating a binding targetting a JTextField. The "text" property of the JTextField is updated anytime the Document of the JTextComponent changes.
   Binding binding = new Binding(source, "sourcePath", jTextField, "text",
           SwingBindingSupport.TextChangeStrategyParameter, CHANGE_ON_TYPE);
 

JTree

The primary property to bind to is the "root" property. The "root" specifies the root object of the tree (or more correctly, the TreeModel). Use child bindings to specify how children of the root, and other objects in the graph, are obtained. The child bindings must resolve to Lists, and preferrably ObservableLists. The following illustrates creating a binding targetting a JTree. For nodes in the tree of type Manager, the "reports" property is used to locate children.
   Manager root;
   Binding binding = new Binding(root, null, jTree, "root");
   // For all nodes of type Manager, use the 'reports' property to find
   // their children.
   binding.addBinding("reports", null,
           SwingBindingSupport.TreeNodeClassParameter, Manager.class);
 


Nested Class Summary
static class SwingBindingSupport.TextChangeStrategy
          An enumeration of the possible values for the TextChangeStrategyParameter of text components.
 
Field Summary
static Binding.Parameter<java.lang.String> ComboBoxSelectedObjectPropertyParameter
          A Binding.Parameter used to specify whether the child binding identifies the path for the "selectedObjectProperty" key of a JComboBox.
static Binding.Parameter<java.lang.Boolean> DisableOnIncompletePathParameter
          A Binding.Parameter used to specify whether the target component should disable itself when the source path is incomplete.
static Binding.Parameter<java.lang.Boolean> EmptyNodeTreatedAsLeafParameter
          A Binding.Parameter used to specify whether a node with no children is treated as a leaf.
static Binding.Parameter<java.lang.Class> TableColumnClassParameter
          A Binding.Parameter used to specify the class of the table column.
static Binding.Parameter<java.lang.Integer> TableColumnParameter
          A Binding.Parameter used to specify the column the binding applies to.
static Binding.Parameter<SwingBindingSupport.TextChangeStrategy> TextChangeStrategyParameter
          A Binding.Parameter used to specify when the "text" property of text components should change.
static Binding.Parameter<java.lang.Class> TreeNodeClassParameter
          A Binding.Parameter used to specify the class a child binding applies to.
 
Method Summary
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TextChangeStrategyParameter

public static final Binding.Parameter<SwingBindingSupport.TextChangeStrategy> TextChangeStrategyParameter
A Binding.Parameter used to specify when the "text" property of text components should change. If not specified, the default value is CHANGE_ON_ACTION_OR_FOCUS_LOST.

See Also:
Binding.setValue(Binding.Parameter,Object)

DisableOnIncompletePathParameter

public static final Binding.Parameter<java.lang.Boolean> DisableOnIncompletePathParameter
A Binding.Parameter used to specify whether the target component should disable itself when the source path is incomplete. If not specified, the default is true. This property is supported for JCheckBox's "selected" property, JSlider's "value" property, and JTextComponent's "text" property.l

See Also:
Binding.setValue(Binding.Parameter,Object)

TableColumnParameter

public static final Binding.Parameter<java.lang.Integer> TableColumnParameter
A Binding.Parameter used to specify the column the binding applies to. This is used on child bindings where the target is a JTable. If not specified, an IllegalArgumentException is thrown when bound.

See Also:
Binding.setValue(Binding.Parameter,Object)

TableColumnClassParameter

public static final Binding.Parameter<java.lang.Class> TableColumnClassParameter
A Binding.Parameter used to specify the class of the table column. This is used on child bindings where the target is a JTable. If not specified, the column class is treated as Object.class.

See Also:
Binding.setValue(Binding.Parameter,Object)

EmptyNodeTreatedAsLeafParameter

public static final Binding.Parameter<java.lang.Boolean> EmptyNodeTreatedAsLeafParameter
A Binding.Parameter used to specify whether a node with no children is treated as a leaf. The default is false.

See Also:
Binding.setValue(Binding.Parameter,Object)

TreeNodeClassParameter

public static final Binding.Parameter<java.lang.Class> TreeNodeClassParameter
A Binding.Parameter used to specify the class a child binding applies to. If not specified, an IllegalArgumentException is thrown when bound.

See Also:
Binding.setValue(Binding.Parameter,Object)

ComboBoxSelectedObjectPropertyParameter

public static final Binding.Parameter<java.lang.String> ComboBoxSelectedObjectPropertyParameter
A Binding.Parameter used to specify whether the child binding identifies the path for the "selectedObjectProperty" key of a JComboBox. See the description of how to bind to a JComboBox for more information.

See Also:
Binding.setValue(Binding.Parameter,Object)