Java Reference
In-Depth Information
When.BooleanConditionBuilder
For
BooleanBinding otherwise(ObservableBooleanValue b)
BooleanBinding otherwise(boolean b)
When.StringConditionBuilder
For
StringBinding otherwise(ObservableStringValue str)
StringBinding otherwise(String str)
When.ObjectConditionBuilder
For
ObjectBinding<T> otherwise(ObservableObjectValue<T> obj)
ObjectBinding<T> otherwise(T obj)
The net effect of these method signatures is that you can build up a binding that resembles an if/then/else
expression this way:
new When(b).then(x).otherwise(y)
where b is an ObservableBooleanValue , and x and y are of similar types and can be either observable or
unobservable. The resulting binding will be of a type similar to that of x and y .
The program in Listing 4-7 uses the fluent interface API from the When class to calculate the area of a triangle with
given sides a , b , and c . Recall that to form a triangle, the three sides must satisfy the following conditions:
a + b > c, b + c > a, c + a > b.
When the preceding conditions are satisfied, the area of the triangle can be calculated using Heron's formula:
Area = sqrt(s * (s - a) * (s - b) * (s - c))
where s is the semiperimeter:
s = (a + b + c) / 2.
Listing 4-7. HeronsFormulaExample.java
import javafx.beans.binding.DoubleBinding;
import javafx.beans.binding.When;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
public class HeronsFormulaExample {
public static void main(String[] args) {
DoubleProperty a = new SimpleDoubleProperty(0);
DoubleProperty b = new SimpleDoubleProperty(0);
DoubleProperty c = new SimpleDoubleProperty(0);
DoubleBinding s = a.add(b).add(c).divide(2.0D);
final DoubleBinding areaSquared = new When(
a.add(b).greaterThan(c)
.and(b.add(c).greaterThan(a))
 
Search WWH ::




Custom Search