com.jgoodies.binding.adapter
Class Bindings

java.lang.Object
  extended by com.jgoodies.binding.adapter.Bindings

public final class Bindings
extends Object

Consists only of static methods that bind Swing components to ValueModels. This is one of two helper classes that assist you in establishing a binding: 1) this Bindings class binds components that have been created before; it wraps ValueModels with the adapters from package com.jgoodies.binding.adapter. 2) the BasicComponentFactory creates Swing components that are then bound using this Bindings class.

If you have an existing factory that vends Swing components, you can use this Bindings class to bind them to ValueModels. If you don't have such a factory, you can use the BasicComponentFactory or a custom subclass to create and bind Swing components.

The binding process for JCheckBox, JRadioButton, and other AbstractButtons retains the former enablement state. Before the new (adapting) model is set, the enablement is requested from the model, not the button. This enablement is set after the new model has been set.

TODO: Consider adding binding methods for JProgressBar, JSlider, JSpinner, and JTabbedPane.

TODO: Consider adding connection methods for pairs of bean properties. In addition to the PropertyConnector's #connect method, this could add boolean operators such as: not, and, or, nor.

Version:
$Revision: 1.11 $
Author:
Karsten Lentzsch
See Also:
ValueModel, BasicComponentFactory

Method Summary
static void addComponentPropertyHandler(JComponent component, ValueModel valueModel)
          If the given model is a ComponentValueModel, a component property handler is registered with this model.
static void bind(JCheckBoxMenuItem checkBoxMenuItem, ValueModel valueModel)
          Binds a JCheckBoxMenuItem to the given ValueModel and retains the enablement state.
static void bind(JCheckBox checkBox, ValueModel valueModel)
          Binds a JCheckBox to the given ValueModel and retains the enablement state.
static void bind(JColorChooser colorChooser, ValueModel valueModel)
          Binds a JColorChooser to the given Color-typed ValueModel.
static void bind(JColorChooser colorChooser, ValueModel valueModel, Color defaultColor)
          Binds a JColorChooser to the given Color-typed ValueModel.
static void bind(JComboBox comboBox, SelectionInList selectionInList)
          Binds a non-editable JComboBox to the given SelectionInList using the SelectionInList's ListModel as list data provider and the SelectionInList's selection index holder for the combo box model's selected item.
static void bind(JFormattedTextField textField, ValueModel valueModel)
          Binds the given JFormattedTextField to the specified ValueModel.
static void bind(JLabel label, ValueModel valueModel)
          Binds the given JLabel to the specified ValueModel.
static void bind(JList list, SelectionInList selectionInList)
          Binds a JList to the given SelectionInList using the SelectionInList's ListModel as list data provider and the SelectionInList's selection index holder for the selection model.
static void bind(JRadioButtonMenuItem radioButtonMenuItem, ValueModel model, Object choice)
          Binds a JRadioButtonMenuItem to the given ValueModel and retains the enablement state.
static void bind(JRadioButton radioButton, ValueModel model, Object choice)
          Binds a JRadioButton to the given ValueModel and retains the enablement state.
static void bind(JTextArea textArea, ValueModel valueModel)
          Binds a text area to the given ValueModel.
static void bind(JTextArea textArea, ValueModel valueModel, boolean commitOnFocusLost)
          Binds a text area to the given ValueModel.
static void bind(JTextArea textArea, ValueModel valueModel, Document document, boolean commitOnFocusLost)
          Binds a text area to the given ValueModel.
static void bind(JTextField textField, ValueModel valueModel)
          Bind a text fields or password field to the given ValueModel.
static void bind(JTextField textField, ValueModel valueModel, boolean commitOnFocusLost)
          Binds a text field or password field to the given ValueModel.
static void bind(JTextField textField, ValueModel valueModel, Document document, boolean commitOnFocusLost)
          Binds a text field or password field to the given ValueModel.
static void removeComponentPropertyHandler(JComponent component)
          If the given component holds a ComponentValueModel and a ComponentPropertyHandler in its client properties, the handler is removed as listener from the model, and the model and handler are removed from the client properties.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

bind

public static void bind(JCheckBox checkBox,
                        ValueModel valueModel)
Binds a JCheckBox to the given ValueModel and retains the enablement state. The bound check box is selected if and only if the model's value equals Boolean.TRUE.

The value model is converted to the required interface ToggleButtonModel using a ToggleButtonAdapter.

Parameters:
checkBox - the check box to be bound
valueModel - the model that provides a Boolean value
Throws:
NullPointerException - if the checkBox or valueModel is null

bind

public static void bind(JCheckBoxMenuItem checkBoxMenuItem,
                        ValueModel valueModel)
Binds a JCheckBoxMenuItem to the given ValueModel and retains the enablement state. The bound menu item is selected if and only if the model's value equals Boolean.TRUE.

Note: For users of the JGoodies UIF (user interface framework) the recommended way to create and bind check box menu items is the class com.jgoodies.uif.ToggleAction.

The value model is converted to the required interface ToggleButtonModel using a ToggleButtonAdapter.

Parameters:
checkBoxMenuItem - the check box menu item to be bound
valueModel - the model that provides a Boolean value
Throws:
NullPointerException - if the menu item or valueModel is null

bind

public static void bind(JColorChooser colorChooser,
                        ValueModel valueModel)
Binds a JColorChooser to the given Color-typed ValueModel. The ValueModel must be of type Color and must allow read-access to its value.

Also, it is strongly recommended (though not required) that the underlying ValueModel provides only non-null values. This is so because the ColorSelectionModel behavior is undefined for null values and it may have unpredictable results. To avoid these problems, you may bind the ColorChooser with a default color using bind(JColorChooser, ValueModel, Color). Note: There's a bug in Java 1.4.2, Java 5 and Java 6 that affects this binding. The BasicColorChooserUI doesn't listen to changes in the selection model, and so the preview panel won't update if the selected color changes. As a workaround you can use BasicComponentFactory.createColorChooser(ValueModel), or you could use a Look&Feel that fixes the bug mentioned above.

Parameters:
colorChooser - the color chooser to be bound
valueModel - the model that provides non-null Color values.
Throws:
NullPointerException - if the color chooser or value model is null
Since:
1.0.3
See Also:
bind(JColorChooser, ValueModel, Color)

bind

public static void bind(JColorChooser colorChooser,
                        ValueModel valueModel,
                        Color defaultColor)
Binds a JColorChooser to the given Color-typed ValueModel. The ValueModel must be of type Color and must allow read-access to its value. The default color will be used if the valueModel returns null.

Note: There's a bug in Java 1.4.2, Java 5 and Java 6 that affects this binding. The BasicColorChooserUI doesn't listen to changes in the selection model, and so the preview panel won't update if the selected color changes. As a workaround you can use BasicComponentFactory.createColorChooser(ValueModel), or you could use a Look&Feel that fixes the bug mentioned above.

Parameters:
colorChooser - the color chooser to be bound
valueModel - the model that provides non-null Color values.
defaultColor - the color used if the valueModel returns null
Throws:
NullPointerException - if the color chooser, value model, or default color is null
Since:
1.1

bind

public static void bind(JComboBox comboBox,
                        SelectionInList selectionInList)
Binds a non-editable JComboBox to the given SelectionInList using the SelectionInList's ListModel as list data provider and the SelectionInList's selection index holder for the combo box model's selected item.

There are a couple of other possibilities to bind a JComboBox. See the constructors and the class comment of the ComboBoxAdapter.

Parameters:
comboBox - the combo box to be bound
selectionInList - provides the list and selection
Throws:
NullPointerException - if the combo box or the selectionInList is null
Since:
1.0.1
See Also:
ComboBoxAdapter

bind

public static void bind(JFormattedTextField textField,
                        ValueModel valueModel)
Binds the given JFormattedTextField to the specified ValueModel. Synchronized the ValueModel's value with the text field's value by means of a PropertyConnector.

Parameters:
textField - the JFormattedTextField that is to be bound
valueModel - the model that provides the value
Throws:
NullPointerException - if the text field or valueModel is null

bind

public static void bind(JLabel label,
                        ValueModel valueModel)
Binds the given JLabel to the specified ValueModel.

Parameters:
label - a label that shall be bound to the given value model
valueModel - the model that provides the value
Throws:
NullPointerException - if the label or valueModel is null

bind

public static void bind(JList list,
                        SelectionInList selectionInList)
Binds a JList to the given SelectionInList using the SelectionInList's ListModel as list data provider and the SelectionInList's selection index holder for the selection model.

Parameters:
list - the list to be bound
selectionInList - provides the list and selection
Throws:
NullPointerException - if the list or the selectionInList is null

bind

public static void bind(JRadioButton radioButton,
                        ValueModel model,
                        Object choice)
Binds a JRadioButton to the given ValueModel and retains the enablement state. The bound radio button is selected if and only if the model's value equals the specified choice value.

The model is converted to the required interface ToggleButtonModel using a RadioButtonAdapter.

Parameters:
radioButton - the radio button to be bound to the given model
model - the model that provides the current choice
choice - this button's value
Throws:
NullPointerException - if the valueModel is null

bind

public static void bind(JRadioButtonMenuItem radioButtonMenuItem,
                        ValueModel model,
                        Object choice)
Binds a JRadioButtonMenuItem to the given ValueModel and retains the enablement state. The bound menu item is selected if and only if the model's value equals the specified choice.

Note: For users of the JGoodies UIF (user interface framework) the recommended way to create and bind radio button menu items is the class com.jgoodies.uif.ToggleAction.

The model is converted to the required interface ToggleButtonModel using a RadioButtonAdapter.

Parameters:
radioButtonMenuItem - the radio item to be bound to the given model
model - the model that provides the current choice
choice - this button's value
Throws:
NullPointerException - if the valueModel is null

bind

public static void bind(JTextArea textArea,
                        ValueModel valueModel)
Binds a text area to the given ValueModel. The model is updated on every character typed.

TODO: Consider changing the semantics to commit on focus lost. This would be consistent with the text component vending factory methods in the BasicComponentFactory that have no boolean parameter.

Parameters:
textArea - the text area to be bound to the value model
valueModel - the model that provides the text value
Throws:
NullPointerException - if the text component or valueModel is null

bind

public static void bind(JTextArea textArea,
                        ValueModel valueModel,
                        boolean commitOnFocusLost)
Binds a text area to the given ValueModel. The model can be updated on focus lost or on every character typed. The DocumentAdapter used in this binding doesn't filter newlines.

Parameters:
textArea - the text area to be bound to the value model
valueModel - the model that provides the text value
commitOnFocusLost - true to commit text changes on focus lost, false to commit text changes on every character typed
Throws:
NullPointerException - if the text component or valueModel is null

bind

public static void bind(JTextArea textArea,
                        ValueModel valueModel,
                        Document document,
                        boolean commitOnFocusLost)
Binds a text area to the given ValueModel. The model can be updated on focus lost or on every character typed. The DocumentAdapter used in this binding stores the contents in the given Document and doesn't filter newlines.

Parameters:
textArea - the text area to be bound to the value model
valueModel - the model that provides the text value
document - the Document used to store the contents
commitOnFocusLost - true to commit text changes on focus lost, false to commit text changes on every character typed
Throws:
NullPointerException - if the component, valueModel, or document is null

bind

public static void bind(JTextField textField,
                        ValueModel valueModel)
Bind a text fields or password field to the given ValueModel. The model is updated on every character typed.

Security Note: If you use this method to bind a JPasswordField, the field's password will be requested as Strings from the field and will be held as String by the given ValueModel. These password String could potentially be observed in a security fraud. For stronger security it is recommended to request a character array from the JPasswordField and clear the array after use by setting each character to zero. Method JPasswordField.getPassword() return's the field's password as a character array.

TODO: Consider changing the semantics to commit on focus lost. This would be consistent with the text component vending factory methods in the BasicComponentFactory that have no boolean parameter.

Parameters:
textField - the text field to be bound to the value model
valueModel - the model that provides the text value
Throws:
NullPointerException - if the text component or valueModel is null
See Also:
JPasswordField.getPassword()

bind

public static void bind(JTextField textField,
                        ValueModel valueModel,
                        boolean commitOnFocusLost)
Binds a text field or password field to the given ValueModel. The model can be updated on focus lost or on every character typed. The DocumentAdapter used in this binding filters newlines.

Security Note: If you use this method to bind a JPasswordField, the field's password will be requested as Strings from the field and will be held as String by the given ValueModel. These password String could potentially be observed in a security fraud. For stronger security it is recommended to request a character array from the JPasswordField and clear the array after use by setting each character to zero. Method JPasswordField.getPassword() return's the field's password as a character array.

Parameters:
textField - the text field to be bound to the value model
valueModel - the model that provides the text value
commitOnFocusLost - true to commit text changes on focus lost, false to commit text changes on every character typed
Throws:
NullPointerException - if the text component or valueModel is null
See Also:
JPasswordField.getPassword()

bind

public static void bind(JTextField textField,
                        ValueModel valueModel,
                        Document document,
                        boolean commitOnFocusLost)
Binds a text field or password field to the given ValueModel. The model can be updated on focus lost or on every character typed. The DocumentAdapter used in this binding stores the contents in the given Document and filters newlines.

Security Note: If you use this method to bind a JPasswordField, the field's password will be requested as Strings from the field and will be held as String by the given ValueModel. These password String could potentially be observed in a security fraud. For stronger security it is recommended to request a character array from the JPasswordField and clear the array after use by setting each character to zero. Method JPasswordField.getPassword() return's the field's password as a character array.

Parameters:
textField - the text field to be bound to the value model
valueModel - the model that provides the text value
document - the Document used to store the contents
commitOnFocusLost - true to commit text changes on focus lost, false to commit text changes on every character typed
Throws:
NullPointerException - if the component, valueModel, or document is null
See Also:
JPasswordField.getPassword()

addComponentPropertyHandler

public static void addComponentPropertyHandler(JComponent component,
                                               ValueModel valueModel)
If the given model is a ComponentValueModel, a component property handler is registered with this model. This handler updates the component state if the ComponentValueModel indicates a change in one of its properties, for example: visible, enabled, and editable.

Also the ComponentValueModel and the component handler are stored as client properties with the component. This way they can be removed later using #removeComponentPropertyHandler.

Parameters:
component - the component where the handler is registered
valueModel - the model to observe
Since:
1.1
See Also:
removeComponentPropertyHandler(JComponent), ComponentValueModel

removeComponentPropertyHandler

public static void removeComponentPropertyHandler(JComponent component)
If the given component holds a ComponentValueModel and a ComponentPropertyHandler in its client properties, the handler is removed as listener from the model, and the model and handler are removed from the client properties.

Parameters:
component -
Since:
1.1
See Also:
addComponentPropertyHandler(JComponent, ValueModel), ComponentValueModel


Copyright © 2002-2006 JGoodies Karsten Lentzsch. All Rights Reserved.