Class Bindings
Usually there are two possibilities to define the same operation: the Fluent
API and the the factory methods in this class. This allows a developer to
define complex expression in a way that is most easy to understand. For
instance the expression result = a*b + c*d
can be defined using only
the Fluent API:
DoubleBinding result = a.multiply(b).add(c.multiply(d));
Or using only factory methods in Bindings:
NumberBinding result = add (multiply(a, b), multiply(c,d));
Or mixing both possibilities:
NumberBinding result = add (a.multiply(b), c.multiply(d));
The main difference between using the Fluent API and using the factory
methods in this class is that the Fluent API requires that at least one of
the operands is an Expression (see javafx.beans.binding
). (Every
Expression contains a static method that generates an Expression from an
ObservableValue
.)
Also if you watched closely, you might have noticed that the return type of
the Fluent API is different in the examples above. In a lot of cases the
Fluent API allows to be more specific about the returned type (see
NumberExpression
for more details about implicit
casting.
- Since:
- JavaFX 2.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic BooleanBinding
and
(ObservableBooleanValue op1, ObservableBooleanValue op2) Creates aBooleanBinding
that calculates the conditional-AND operation on the value of two instance ofObservableBooleanValue
.static <T> void
bindBidirectional
(Property<String> stringProperty, Property<T> otherProperty, StringConverter<T> converter) Generates a bidirectional binding (or "bind with inverse") between aString
-Property
and anotherProperty
using the specifiedStringConverter
for conversion.static <T> void
bindBidirectional
(Property<T> property1, Property<T> property2) Generates a bidirectional binding (or "bind with inverse") between two instances ofProperty
.static BooleanBinding
createBooleanBinding
(Callable<Boolean> func, Observable... dependencies) Helper function to create a customBooleanBinding
.static NumberBinding
divide
(ObservableNumberValue op1, int op2) Creates a newNumberBinding
that calculates the division of the value of aObservableNumberValue
and a constant value.static <E> BooleanBinding
isEmpty
(ObservableList<E> op) static BooleanBinding
Creates aBooleanBinding
that calculates the inverse of the value of aObservableBooleanValue
.static void
unbindBidirectional
(Object property1, Object property2) Delete a bidirectional binding that was previously defined withbindBidirectional(Property, Property)
orinvalid @link
#bindBidirectional(javafx.beans.property.Property, javafx.beans.property.Property, java.text.Format)
static <T> void
unbindBidirectional
(Property<T> property1, Property<T> property2) Delete a bidirectional binding that was previously defined withbindBidirectional(Property, Property)
.
-
Method Details
-
createBooleanBinding
public static BooleanBinding createBooleanBinding(Callable<Boolean> func, Observable... dependencies) Helper function to create a customBooleanBinding
.- Parameters:
func
- The function that calculates the value of this bindingdependencies
- The dependencies of this binding- Returns:
- The generated binding
- Since:
- JavaFX 2.1
-
not
Creates aBooleanBinding
that calculates the inverse of the value of aObservableBooleanValue
.- Parameters:
op
- theObservableBooleanValue
- Returns:
- the new
BooleanBinding
- Throws:
NullPointerException
- if the operand isnull
-
bindBidirectional
Generates a bidirectional binding (or "bind with inverse") between two instances ofProperty
.A bidirectional binding is a binding that works in both directions. If two properties
a
andb
are linked with a bidirectional binding and the value ofa
changes,b
is set to the same value automatically. And vice versa, ifb
changes,a
is set to the same value.A bidirectional binding can be removed with
unbindBidirectional(Property, Property)
.Note: this implementation of a bidirectional binding behaves differently from all other bindings here in two important aspects. A property that is linked to another property with a bidirectional binding can still be set (usually bindings would throw an exception). Secondly bidirectional bindings are calculated eagerly, i.e. a bound property is updated immediately.
- Type Parameters:
T
- the types of the properties- Parameters:
property1
- the firstProperty<T>
property2
- the secondProperty<T>
- Throws:
NullPointerException
- if one of the properties isnull
IllegalArgumentException
- if both properties are equal
-
unbindBidirectional
Delete a bidirectional binding that was previously defined withbindBidirectional(Property, Property)
.- Type Parameters:
T
- the types of the properties- Parameters:
property1
- the firstProperty<T>
property2
- the secondProperty<T>
- Throws:
NullPointerException
- if one of the properties isnull
IllegalArgumentException
- if both properties are equal
-
unbindBidirectional
Delete a bidirectional binding that was previously defined withbindBidirectional(Property, Property)
orinvalid @link
#bindBidirectional(javafx.beans.property.Property, javafx.beans.property.Property, java.text.Format)
- Parameters:
property1
- the firstProperty<T>
property2
- the secondProperty<T>
- Throws:
NullPointerException
- if one of the properties isnull
IllegalArgumentException
- if both properties are equal- Since:
- JavaFX 2.1
-
bindBidirectional
public static <T> void bindBidirectional(Property<String> stringProperty, Property<T> otherProperty, StringConverter<T> converter) Generates a bidirectional binding (or "bind with inverse") between aString
-Property
and anotherProperty
using the specifiedStringConverter
for conversion.A bidirectional binding is a binding that works in both directions. If two properties
a
andb
are linked with a bidirectional binding and the value ofa
changes,b
is set to the same value automatically. And vice versa, ifb
changes,a
is set to the same value.A bidirectional binding can be removed with
unbindBidirectional(Object, Object)
.Note: this implementation of a bidirectional binding behaves differently from all other bindings here in two important aspects. A property that is linked to another property with a bidirectional binding can still be set (usually bindings would throw an exception). Secondly bidirectional bindings are calculated eagerly, i.e. a bound property is updated immediately.
- Parameters:
stringProperty
- theString
Property
otherProperty
- the other (non-String
)Property
converter
- theStringConverter
used to convert between the properties- Throws:
NullPointerException
- if one of the properties or theconverter
isnull
IllegalArgumentException
- if both properties are equal- Since:
- JavaFX 2.1
-
and
Creates aBooleanBinding
that calculates the conditional-AND operation on the value of two instance ofObservableBooleanValue
.- Parameters:
op1
- firstObservableBooleanValue
op2
- secondObservableBooleanValue
- Returns:
- the new
BooleanBinding
- Throws:
NullPointerException
- if one of the operands isnull
-
divide
Creates a newNumberBinding
that calculates the division of the value of aObservableNumberValue
and a constant value.- Parameters:
op1
- the constant valueop2
- theObservableNumberValue
- Returns:
- the new
NumberBinding
- Throws:
NullPointerException
- if theObservableNumberValue
isnull
-
isEmpty
- Type Parameters:
E
- type of theList
elements- Parameters:
op
- theObservableList
- Returns:
- the new
BooleanBinding
- Throws:
NullPointerException
- if theObservableList
isnull
- Since:
- JavaFX 2.1
-