Java Reference
In-Depth Information
Notice how the 13 lines of code and 12 intermediate variables used in Listing 4-5 to build up the area binding are
reduced to the 7 lines of code with no intermediate variables used in Listing 4-6. We also used the
Bindings.format()
method to build up a
StringExpression
object called
output
. There are two overloaded
Bindings.format()
methods
with signatures:
StringExpression format(Locale locale, String format, Object... args)
StringExpression format(String format, Object... args)
They work similarly to the corresponding
String.format()
methods in that they format the values
args
according to the format specification
format
and the
Locale locale
, or the default
Locale
. If any of the
args
is an
ObservableValue
, its change is reflected in the
StringExpression
.
When we run the program in Listing 4-6, the following output is printed to the console:
For A(0,0), B(6,0), C(4,3), the area of triangle ABC is 9.0
For A(1,0), B(2,2), C(0,1), the area of triangle ABC is 1.5
Next we unravel the mystery of the
When
class and the role it plays in constructing bindings that are essentially
if/then/else expressions. The
When
class has a constructor that takes an
ObservableBooleanValue
argument:
public When(ObservableBooleanValue b)
It has the following 11 overloaded
then()
methods.
When.NumberConditionBuilder then(ObservableNumberValue n)
When.NumberConditionBuilder then(double d)
When.NumberConditionBuilder then(float f)
When.NumberConditionBuilder then(long l)
When.NumberConditionBuilder then(int i)
When.BooleanConditionBuilder then(ObservableBooleanValue b)
When.BooleanConditionBuilder then(boolean b)
When.StringConditionBuilder then(ObservableStringValue str)
When.StringConditionBuilder then(String str)
When.ObjectConditionBuilder<T> then(ObservableObjectValue<T> obj)
When.ObjectConditionBuilder<T> then(T obj)
The type of object returned from the
then()
method depends on the type of the argument. If the argument is a
numeric type, either observable or unobservable, the return type is a nested class
When.NumberConditionBuilder
.
Similarly, for Boolean arguments, the return type is
When.BooleanConditionBuilder
; for string arguments,
When.StringConditionBuilder
; and for object arguments,
When.ObjectConditionBuilder
.
These condition builders in turn have the following
otherwise()
methods.
When.NumberConditionBuilder
•
For
NumberBinding otherwise(ObservableNumberValue n)
•
DoubleBinding otherwise(double d)
•
NumberBinding otherwise(float f)
•
NumberBinding otherwise(long l)
•
NumberBinding otherwise(int i)
•
Search WWH ::
Custom Search