Java Reference
In-Depth Information
Understanding the Bindings Utility Class
The Bindings class contains 236 factory methods that make new bindings out of existing observable values and
regular values. Most of the methods are overloaded to take into account that both observable values and regular Java
(unobservable) values can be used to build new bindings. At least one of the parameters must be an observable value.
Here are the signatures of the nine overloaded add() methods:
public static NumberBinding add(ObservableNumberValue n1, ObservableNumberValue n2)
public static DoubleBinding add(ObservableNumberValue n, double d)
public static DoubleBinding add(double d, ObservableNumberValue n)
public static NumberBinding add(ObservableNumberValue n, float f)
public static NumberBinding add(float f, ObservableNumberValue n)
public static NumberBinding add(ObservableNumberValue n, long l)
public static NumberBinding add(long l, ObservableNumberValue n)
public static NumberBinding add(ObservableNumberValue n, int i)
public static NumberBinding add(int i, ObservableNumberValue n)
When the add() method is called, it returns a NumberBinding with dependencies that include all the observable
value parameters, and whose value is the sum of the value of its two parameters. Similarly overloaded methods exist
for subtract() , multiply() , and divide() .
recall from the last section that ObservableIntegerValue , ObservableLongValue , ObservableFloatValue ,
and ObservableDoubleValue are subclasses of ObservableNumberValue . therefore the four arithmetic methods just
mentioned can take any combinations of these observable numeric values as well as any unobservable values.
Note
The program in Listing 4-5 uses the arithmetic methods in Bindings to calculate the area of a triangle in the
Cartesian plane with vertices (x1, y1) , (x2, y2) , (x3, y3) using this formula:
Area = (x1*y2 + x2*y3 + x3*y1 - x1*y3 - x2*y1 - x3*y2) / 2
Listing 4-5. TriangleAreaExample.java
import javafx.beans.binding.Bindings;
import javafx.beans.binding.NumberBinding;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
public class TriangleAreaExample {
public static void main(String[] args) {
IntegerProperty x1 = new SimpleIntegerProperty(0);
IntegerProperty y1 = new SimpleIntegerProperty(0);
IntegerProperty x2 = new SimpleIntegerProperty(0);
IntegerProperty y2 = new SimpleIntegerProperty(0);
IntegerProperty x3 = new SimpleIntegerProperty(0);
IntegerProperty y3 = new SimpleIntegerProperty(0);
final NumberBinding x1y2 = Bindings.multiply(x1, y2);
final NumberBinding x2y3 = Bindings.multiply(x2, y3);
 
 
Search WWH ::




Custom Search