Class Bindings

java.lang.Object
javafx.beans.binding.Bindings

public final class Bindings extends Object
Bindings is a helper class with a lot of utility functions to create simple 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 Details

    • createBooleanBinding

      public static BooleanBinding createBooleanBinding(Callable<Boolean> func, Observable... dependencies)
      Helper function to create a custom BooleanBinding.
      Parameters:
      func - The function that calculates the value of this binding
      dependencies - The dependencies of this binding
      Returns:
      The generated binding
      Since:
      JavaFX 2.1
    • not

      public static BooleanBinding not(ObservableBooleanValue op)
      Creates a BooleanBinding that calculates the inverse of the value of a ObservableBooleanValue.
      Parameters:
      op - the ObservableBooleanValue
      Returns:
      the new BooleanBinding
      Throws:
      NullPointerException - if the operand is null
    • bindBidirectional

      public static <T> void bindBidirectional(Property<T> property1, Property<T> property2)
      Generates a bidirectional binding (or "bind with inverse") between two instances of Property.

      A bidirectional binding is a binding that works in both directions. If two properties a and b are linked with a bidirectional binding and the value of a changes, b is set to the same value automatically. And vice versa, if b 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 first Property<T>
      property2 - the second Property<T>
      Throws:
      NullPointerException - if one of the properties is null
      IllegalArgumentException - if both properties are equal
    • unbindBidirectional

      public static <T> void unbindBidirectional(Property<T> property1, Property<T> property2)
      Delete a bidirectional binding that was previously defined with bindBidirectional(Property, Property).
      Type Parameters:
      T - the types of the properties
      Parameters:
      property1 - the first Property<T>
      property2 - the second Property<T>
      Throws:
      NullPointerException - if one of the properties is null
      IllegalArgumentException - if both properties are equal
    • unbindBidirectional

      public static void unbindBidirectional(Object property1, Object property2)
      Delete a bidirectional binding that was previously defined with bindBidirectional(Property, Property) or
      invalid @link
      #bindBidirectional(javafx.beans.property.Property, javafx.beans.property.Property, java.text.Format)
      .
      Parameters:
      property1 - the first Property<T>
      property2 - the second Property<T>
      Throws:
      NullPointerException - if one of the properties is null
      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 a String-Property and another Property using the specified StringConverter for conversion.

      A bidirectional binding is a binding that works in both directions. If two properties a and b are linked with a bidirectional binding and the value of a changes, b is set to the same value automatically. And vice versa, if b 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 - the String Property
      otherProperty - the other (non-String) Property
      converter - the StringConverter used to convert between the properties
      Throws:
      NullPointerException - if one of the properties or the converter is null
      IllegalArgumentException - if both properties are equal
      Since:
      JavaFX 2.1
    • and

      Creates a BooleanBinding that calculates the conditional-AND operation on the value of two instance of ObservableBooleanValue.
      Parameters:
      op1 - first ObservableBooleanValue
      op2 - second ObservableBooleanValue
      Returns:
      the new BooleanBinding
      Throws:
      NullPointerException - if one of the operands is null
    • divide

      public static NumberBinding divide(ObservableNumberValue op1, int op2)
      Creates a new NumberBinding that calculates the division of the value of a ObservableNumberValue and a constant value.
      Parameters:
      op1 - the constant value
      op2 - the ObservableNumberValue
      Returns:
      the new NumberBinding
      Throws:
      NullPointerException - if the ObservableNumberValue is null