com.jgoodies.binding.adapter

Class BasicComponentFactory


public class BasicComponentFactory
extends java.lang.Object

Consists only of static methods that create and vend frequently used Swing components that are then bound to a given ValueModel. This class is one of two helper classes that help you establish a binding: 1) the Bindings class binds components that have been created before; it wraps ValueModels with the adapters from package com.jgoodies.binding.adapter. 2) this BasicComponentFactory creates Swing components that are then tied to ValueModels using the the different #bind methods in the Bindings class.

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

This class is intended to be used or extended by custom ComponentFactory classes. Such a factory can create a broader variety of component types, may use different default configurations, and can use your favorite Formatters, FormatterFactories, etc.

Version:
$Revision: 1.14 $
Author:
Karsten Lentzsch
See Also:
ValueModel, Bindings

Constructor Summary

BasicComponentFactory()

Method Summary

static
JComboBox createComboBox(SelectionInList selectionInList)
Creates and returns a non-editable JComboBox that is bound to the given SelectionInList.
static
JComboBox createComboBox(SelectionInList selectionInList, ListCellRenderer cellRenderer)
Creates and returns a non-editable JComboBox that is bound to the given SelectionInList using the given cell renderer.
static
JList createList(SelectionInList selectionInList)
Creates and returns a JList for the given SelectionInList.
static
JList createList(SelectionInList selectionInList, ListCellRenderer cellRenderer)
Creates and returns a JList for the given SelectionInList using the specified optional ListCellRenderer to render cells.
static JCheckBox
createCheckBox(ValueModel valueModel, String text)
Creates and returns a check box with the specified text label that is bound to the given ValueModel.
static JColorChooser
createColorChooser(ValueModel valueModel)
Creates and returns a JColorChooser that has the color selection bound to the given ValueModel.
static JColorChooser
createColorChooser(ValueModel valueModel, Color defaultColor)
Creates and returns a JColorChooser that has the color selection bound to the given ValueModel.
static JFormattedTextField
createDateField(ValueModel valueModel)
Creates and returns a formatted text field that is bound to the Date value of the given ValueModel.
static JFormattedTextField
createFormattedTextField(ValueModel valueModel, Format format)
Creates and returns a formatted text field that binds its value to the given model and converts Strings to values using the given Format.
static JFormattedTextField
createFormattedTextField(ValueModel valueModel, JFormattedTextField.AbstractFormatter formatter)
Creates and returns a formatted text field that binds its value to the given model and converts Strings to values using the given Formatter.
static JFormattedTextField
createFormattedTextField(ValueModel valueModel, JFormattedTextField.AbstractFormatterFactory formatterFactory)
Creates and returns a formatted text field that binds its value to the given model and converts Strings to values using Formatters provided by the given AbstractFormatterFactory.
static JFormattedTextField
createFormattedTextField(ValueModel valueModel, String mask)
Creates and returns a formatted text field that binds its value to the given model and converts Strings to values using a MaskFormatter that is based on the given mask.
static JFormattedTextField
createIntegerField(ValueModel valueModel)
Creates and returns a formatted text field that is bound to the Integer value of the given ValueModel.
static JFormattedTextField
createIntegerField(ValueModel valueModel, NumberFormat numberFormat)
Creates and returns a formatted text field that is bound to the Integer value of the given ValueModel.
static JFormattedTextField
createIntegerField(ValueModel valueModel, NumberFormat numberFormat, Integer emptyNumber)
Creates and returns a formatted text field that is bound to the Integer value of the given ValueModel.
static JFormattedTextField
createIntegerField(ValueModel valueModel, NumberFormat numberFormat, int emptyNumber)
Creates and returns a formatted text field that is bound to the Integer value of the given ValueModel.
static JFormattedTextField
createIntegerField(ValueModel valueModel, int emptyNumber)
Creates and returns a formatted text field that is bound to the Integer value of the given ValueModel.
static JLabel
createLabel(ValueModel valueModel)
Creates and returns a text label that is bound to the given ValueModel.
static JLabel
createLabel(ValueModel valueModel, Format format)
Creates and returns a text label that is bound to the given ValueModel that is wrapped by a StringConverter.
static JFormattedTextField
createLongField(ValueModel valueModel)
Creates and returns a formatted text field that is bound to the Long value of the given ValueModel.
static JFormattedTextField
createLongField(ValueModel valueModel, NumberFormat numberFormat)
Creates and returns a formatted text field that is bound to the Long value of the given ValueModel.
static JFormattedTextField
createLongField(ValueModel valueModel, NumberFormat numberFormat, Long emptyNumber)
Creates and returns a formatted text field that is bound to the Long value of the given ValueModel.
static JFormattedTextField
createLongField(ValueModel valueModel, NumberFormat numberFormat, long emptyNumber)
Creates and returns a formatted text field that is bound to the Long value of the given ValueModel.
static JFormattedTextField
createLongField(ValueModel valueModel, long emptyNumber)
Creates and returns a formatted text field that is bound to the Long value of the given ValueModel.
static JPasswordField
createPasswordField(ValueModel valueModel)
Creates and returns a JPasswordField with the content bound to the given ValueModel.
static JPasswordField
createPasswordField(ValueModel valueModel, boolean commitOnFocusLost)
Creates and returns a JPasswordField with the content bound to the given ValueModel.
static JRadioButton
createRadioButton(ValueModel model, Object choice, String text)
Creates and returns a radio button with the specified text label that is bound to the given ValueModel.
static JTextArea
createTextArea(ValueModel valueModel)
Creates and returns a text area with the content bound to the given ValueModel.
static JTextArea
createTextArea(ValueModel valueModel, boolean commitOnFocusLost)
Creates and returns a text area with the content bound to the given ValueModel.
static JTextField
createTextField(ValueModel valueModel)
Creates and returns a text field with the content bound to the given ValueModel.
static JTextField
createTextField(ValueModel valueModel, boolean commitOnFocusLost)
Creates and returns a text field with the content bound to the given ValueModel.

Constructor Details

BasicComponentFactory

protected BasicComponentFactory()

Method Details

JComboBox createComboBox

public static  JComboBox createComboBox(SelectionInList selectionInList)
Parameters:
selectionInList - provides the list and selection
Returns:
a non-editable JComboBox that is bound to the SelectionInList
Since:
1.0.1

JComboBox createComboBox

public static  JComboBox createComboBox(SelectionInList selectionInList,
                                           ListCellRenderer cellRenderer)
Parameters:
selectionInList - provides the list and selection
cellRenderer - an optional ListCellRenderer, can be null
Returns:
a non-editable JComboBox that is bound to the SelectionInList and uses the given renderer - if non-null
Since:
1.0.1

JList createList

public static  JList createList(SelectionInList selectionInList)
Parameters:
selectionInList - provides the list and selection
Returns:
a JList bound to the given SelectionInList

JList createList

public static  JList createList(SelectionInList selectionInList,
                                   ListCellRenderer cellRenderer)
Parameters:
selectionInList - provides the list and selection
cellRenderer - an optional ListCellRenderer, can be null
Returns:
a JList bound to the given SelectionInList

createCheckBox

public static JCheckBox createCheckBox(ValueModel valueModel,
                                       String text)
Creates and returns a check box with the specified text label that is bound to the given ValueModel. The check box is selected if and only if the model's value equals Boolean.TRUE.

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

Parameters:
valueModel - the model that provides a Boolean value
text - the check boxes' text label
Returns:
a check box with the specified text bound to the given model, selected if the model's value equals Boolean.TRUE

createColorChooser

public static JColorChooser createColorChooser(ValueModel valueModel)
Creates and returns a JColorChooser that has the color selection bound to the given ValueModel. The ValueModel must be of type Color and must allow read-access to its value, and the initial value must not be null.

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 create the ColorChooser with a default color using createColorChooser(ValueModel,Color).

Parameters:
valueModel - a Color-typed ValueModel
Returns:
a color chooser with the selected color bound to the given model
Since:
1.0.3

createColorChooser

public static JColorChooser createColorChooser(ValueModel valueModel,
                                               Color defaultColor)
Creates and returns a JColorChooser that has the color selection bound to the given ValueModel. The ValueModel must be of type Color and must allow read-access to its value. If the valueModel returns null, the given default color is used instead. This avoids problems with the ColorSelectionModel that may have unpredictable result for null values.
Parameters:
valueModel - a Color-typed ValueModel
defaultColor - the color used if the valueModel returns null
Returns:
a color chooser with the selected color bound to the given model
Since:
1.1

createDateField

public static JFormattedTextField createDateField(ValueModel valueModel)
Creates and returns a formatted text field that is bound to the Date value of the given ValueModel. The JFormattedTextField is configured with an AbstractFormatter that uses two different DateFormats to edit and display the Date. A SHORT DateFormat with strict checking is used to edit (parse) a date; the DateFormatter's default DateFormat is used to display (format) a date. In both cases null Dates are mapped to the empty String.
Parameters:
valueModel - the model that holds the value to be edited
Returns:
a formatted text field for Date instances that is bound to the given value model

createFormattedTextField

public static JFormattedTextField createFormattedTextField(ValueModel valueModel,
                                                           Format format)
Creates and returns a formatted text field that binds its value to the given model and converts Strings to values using the given Format.
Parameters:
valueModel - the model that provides the value
format - the Format used to convert values into a text representation and vice versa via #format and #parse
Returns:
a formatted text field that is bound to the given value model

createFormattedTextField

public static JFormattedTextField createFormattedTextField(ValueModel valueModel,
                                                           JFormattedTextField.AbstractFormatter formatter)
Creates and returns a formatted text field that binds its value to the given model and converts Strings to values using the given Formatter.
Parameters:
valueModel - the model that provides the value
formatter - the Formatter used to convert values to a text representation and vice versa via #valueToString and #stringToValue
Returns:
a formatted text field that is bound to the given value model

createFormattedTextField

public static JFormattedTextField createFormattedTextField(ValueModel valueModel,
                                                           JFormattedTextField.AbstractFormatterFactory formatterFactory)
Creates and returns a formatted text field that binds its value to the given model and converts Strings to values using Formatters provided by the given AbstractFormatterFactory.
Parameters:
valueModel - the model that provides the value
formatterFactory - provides formatters for different field states that in turn are used to convert values to a text representation and vice versa via #valueToString and #stringToValue
Returns:
a formatted text field that is bound to the given value model

createFormattedTextField

public static JFormattedTextField createFormattedTextField(ValueModel valueModel,
                                                           String mask)
Creates and returns a formatted text field that binds its value to the given model and converts Strings to values using a MaskFormatter that is based on the given mask.
Parameters:
valueModel - the model that provides the value
mask - the mask pattern used to create an instance of MaskFormatter that in turn converts values to Strings and vice versa
Returns:
a bound formatted text field using a MaskFormatter

createIntegerField

public static JFormattedTextField createIntegerField(ValueModel valueModel)
Creates and returns a formatted text field that is bound to the Integer value of the given ValueModel. Empty strings are converted to null and vice versa.

The Format used to convert numbers to strings and vice versa is NumberFormat.getIntegerInstance().

Parameters:
valueModel - the model that holds the value to be edited
Returns:
a formatted text field for Integer instances that is bound to the specified valueModel

createIntegerField

public static JFormattedTextField createIntegerField(ValueModel valueModel,
                                                     NumberFormat numberFormat)
Creates and returns a formatted text field that is bound to the Integer value of the given ValueModel. Empty strings are converted to null and vice versa.
Parameters:
valueModel - the model that holds the value to be edited
numberFormat - used to convert numbers to strings and vice versa
Returns:
a formatted text field for Integer instances that is bound to the specified valueModel

createIntegerField

public static JFormattedTextField createIntegerField(ValueModel valueModel,
                                                     NumberFormat numberFormat,
                                                     Integer emptyNumber)
Creates and returns a formatted text field that is bound to the Integer value of the given ValueModel. Empty strings are converted to the specified empty number.
Parameters:
valueModel - the model that holds the value to be edited
numberFormat - used to convert numbers to strings and vice versa
emptyNumber - an Integer that represents the empty string
Returns:
a formatted text field for Integer instances that is bound to the specified valueModel

createIntegerField

public static JFormattedTextField createIntegerField(ValueModel valueModel,
                                                     NumberFormat numberFormat,
                                                     int emptyNumber)
Creates and returns a formatted text field that is bound to the Integer value of the given ValueModel. Empty strings are converted to the specified empty number.
Parameters:
valueModel - the model that holds the value to be edited
numberFormat - used to convert numbers to strings and vice versa
emptyNumber - an Integer that represents the empty string
Returns:
a formatted text field for Integer instances that is bound to the specified valueModel

createIntegerField

public static JFormattedTextField createIntegerField(ValueModel valueModel,
                                                     int emptyNumber)
Creates and returns a formatted text field that is bound to the Integer value of the given ValueModel. Empty strings are converted to the specified empty number.

The Format used to convert numbers to strings and vice versa is NumberFormat.getIntegerInstance().

Parameters:
valueModel - the model that holds the value to be edited
emptyNumber - an Integer that represents the empty string
Returns:
a formatted text field for Integer instances that is bound to the specified valueModel

createLabel

public static JLabel createLabel(ValueModel valueModel)
Creates and returns a text label that is bound to the given ValueModel.
Parameters:
valueModel - the model that provides the value
Returns:
a text label that is bound to the given value model

createLabel

public static JLabel createLabel(ValueModel valueModel,
                                 Format format)
Creates and returns a text label that is bound to the given ValueModel that is wrapped by a StringConverter. The conversion to Strings uses the specified Format.
Parameters:
valueModel - the model that provides the value
format - the format used to create the StringConverter
Returns:
a text label that is bound to the given value model

createLongField

public static JFormattedTextField createLongField(ValueModel valueModel)
Creates and returns a formatted text field that is bound to the Long value of the given ValueModel. Empty strings are converted to null and vice versa.

The Format used to convert numbers to strings and vice versa is NumberFormat.getIntegerInstance().

Parameters:
valueModel - the model that holds the value to be edited
Returns:
a formatted text field for Long instances that is bound to the specified valueModel

createLongField

public static JFormattedTextField createLongField(ValueModel valueModel,
                                                  NumberFormat numberFormat)
Creates and returns a formatted text field that is bound to the Long value of the given ValueModel. Empty strings are converted to null and vice versa.
Parameters:
valueModel - the model that holds the value to be edited
numberFormat - used to convert numbers to strings and vice versa
Returns:
a formatted text field for Long instances that is bound to the specified valueModel

createLongField

public static JFormattedTextField createLongField(ValueModel valueModel,
                                                  NumberFormat numberFormat,
                                                  Long emptyNumber)
Creates and returns a formatted text field that is bound to the Long value of the given ValueModel. Empty strings are converted to the specified empty number.
Parameters:
valueModel - the model that holds the value to be edited
numberFormat - used to convert numbers to strings and vice versa
emptyNumber - a Long that represents the empty string
Returns:
a formatted text field for Long instances that is bound to the specified valueModel

createLongField

public static JFormattedTextField createLongField(ValueModel valueModel,
                                                  NumberFormat numberFormat,
                                                  long emptyNumber)
Creates and returns a formatted text field that is bound to the Long value of the given ValueModel. Empty strings are converted to the specified empty number.
Parameters:
valueModel - the model that holds the value to be edited
numberFormat - used to convert numbers to strings and vice versa
emptyNumber - a Long that represents the empty string
Returns:
a formatted text field for Long instances that is bound to the specified valueModel

createLongField

public static JFormattedTextField createLongField(ValueModel valueModel,
                                                  long emptyNumber)
Creates and returns a formatted text field that is bound to the Long value of the given ValueModel. Empty strings are converted to the specified empty number.

The Format used to convert numbers to strings and vice versa is NumberFormat.getIntegerInstance().

Parameters:
valueModel - the model that holds the value to be edited
emptyNumber - a Long that represents the empty string
Returns:
a formatted text field for Long instances that is bound to the specified valueModel

createPasswordField

public static JPasswordField createPasswordField(ValueModel valueModel)
Creates and returns a JPasswordField with the content bound to the given ValueModel. Text changes are committed to the model on focus lost.

Security Note: The binding created by this method uses Strings as values of the given ValueModel. The String-typed passwords 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:
valueModel - the model that provides the value
Returns:
a text field that is bound to the given value model
See Also:
createPasswordField(ValueModel,boolean), JPasswordField.getPassword()

createPasswordField

public static JPasswordField createPasswordField(ValueModel valueModel,
                                                 boolean commitOnFocusLost)
Creates and returns a JPasswordField with the content bound to the given ValueModel. Text changes can be committed to the model on focus lost or on every character typed.

Security Note: The binding created by this method uses Strings as values of the given ValueModel. The String-typed passwords 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:
valueModel - the model that provides the value
commitOnFocusLost - true to commit text changes on focus lost, false to commit text changes on every character typed
Returns:
a text field that is bound to the given value model
See Also:
createPasswordField(ValueModel), JPasswordField.getPassword()

createRadioButton

public static JRadioButton createRadioButton(ValueModel model,
                                             Object choice,
                                             String text)
Creates and returns a radio button with the specified text label that is bound to the given ValueModel. The radio button is selected if and only if the model's value equals the specified choice.

The model is converted to the required ToggleButton using a RadioButtonAdapter.

Parameters:
model - the model that provides the current choice
choice - this button's value
text - the radio buttons' text label
Returns:
a radio button with the specified text bound to the given model, selected if the model's value equals the specified choice

createTextArea

public static JTextArea createTextArea(ValueModel valueModel)
Creates and returns a text area with the content bound to the given ValueModel. Text changes are committed to the model on focus lost.
Parameters:
valueModel - the model that provides the value
Returns:
a text area that is bound to the given value model

createTextArea

public static JTextArea createTextArea(ValueModel valueModel,
                                       boolean commitOnFocusLost)
Creates and returns a text area with the content bound to the given ValueModel. Text changes can be committed to the model on focus lost or on every character typed.
Parameters:
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
Returns:
a text area that is bound to the given value model

createTextField

public static JTextField createTextField(ValueModel valueModel)
Creates and returns a text field with the content bound to the given ValueModel. Text changes are committed to the model on focus lost.
Parameters:
valueModel - the model that provides the value
Returns:
a text field that is bound to the given value model

createTextField

public static JTextField createTextField(ValueModel valueModel,
                                         boolean commitOnFocusLost)
Creates and returns a text field with the content bound to the given ValueModel. Text changes can be committed to the model on focus lost or on every character typed.
Parameters:
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
Returns:
a text field that is bound to the given value model

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