Java Reference
In-Depth Information
Since
centerX
is bound to
width.divide(2)
, when
pane
's width is changed,
centerX
automatically updates itself to match
pane
's width / 2.
Listing 14.6 gives another example that demonstrates bindings.
L
ISTING
14.6
BindingDemo.java
1
import
javafx.beans.property.DoubleProperty;
2
import
javafx.beans.property.SimpleDoubleProperty;
3
4
public class
BindingDemo {
5
public static void
main(String[] args) {
6 DoubleProperty d1 =
new
SimpleDoubleProperty(
1
);
7 DoubleProperty d2 =
new
SimpleDoubleProperty(
2
);
8 d1.bind(d2);
9 System.out.println(
"
d1 is
"
+ d1.getValue()
10 +
"
and d2 is
"
+ d2.getValue());
11 d2.setValue(
70.2
);
12 System.out.println(
"
d1 is
"
+ d1.getValue()
13 +
"
and d2 is
"
+ d2.getValue());
14 }
15 }
create a
DoubleProperty
create a
DoubleProperty
bind property
set a new source value
d1 is 2.0 and d2 is 2.0
d1 is 70.2 and d2 is 70.2
The program creates an instance of
DoubleProperty
using
SimpleDoubleProperty(1)
(line 6). Note that
DoubleProperty
,
FloatProperty
,
LongProperty
,
IntegerProperty
, and
BooleanProperty
are abstract classes. Their
concrete subclasses
SimpleDoubleProperty
,
SimpleFloatProperty
,
SimpleLong-
Property
,
SimpleIntegerProperty
, and
SimpleBooleanProperty
are used to cre-
ate instances of these properties. These classes are very much like wrapper classes
Double
,
Float
,
Long
,
Integer
, and
Boolean
with additional features for binding to a source object.
The program binds
d1
with
d2
(line 8). Now the values in
d1
and
d2
are the same. After
setting
d2
to
70.2
(line 11),
d1
also becomes
70.2
(line 13).
The binding demonstrated in this example is known as
unidirectional binding
. Occasion-
ally, it is useful to synchronize two properties so that a change in one property is reflected in
another object, and vice versa. This is called a
bidirectional binding
. If the target and source
are both binding properties and observable properties, they can be bound bidirectionally using
the
bindBidirectional
method.
14.8
What is a binding property? What interface defines a binding property? What inter-
face defines a source object? What are the binding object types for
int
,
long
,
float
,
double
, and
boolean
? Are
Integer
and
Double
binding properties? Can
Integer
and
Double
be used as source objects in a binding?
14.9
Following the JavaFX binding property naming convention, for a binding property
named
age
of the
IntegerProperty
type, what is its value getter method, value
setter method, and property getter method?
14.10
Can you create an object of
IntegerProperty
using
new IntegerProperty(3)
?
If not, what is the correct way to create it? What will the output if line 8 is replaced
by
d1.bind(d2.multiply(2))
in Listing 14.6? What will the output if line 8 is
replaced by
d1.bind(d2.add(2))
in Listing 14.6?
unidirectional binding
bidirectional binding
✓
✓
Check
Point
14.11
What is a unidirectional binding and what is bidirectional binding? Are all binding
properties capable of bidirectional binding? Write a statement to bind property
d1
with property
d2
bidirectionally.
Search WWH ::
Custom Search