Java Reference
In-Depth Information
updates itself once a change is made in the source. A target binds with a source using the
bind
method as follows:
target.bind(source);
The
bind
method is defined in the
javafx.beans.property.Property
interface. A
binding property is an instance of
javafx.beans.property.Property
. A source object is an
instance of the
javafx.beans.value.ObservableValue
interface. An
ObservableValue
is an entity that wraps a value and allows to observe the value for changes.
JavaFX defines binding properties for primitive types and strings. For a
double
/
float
/
long
/
int
/
boolean
value, its binding property type is
DoubleProperty
/
FloatProperty
/
LongProperty
/
IntegerProperty
/
BooleanProperty
. For a string, its binding property
type is
StringProperty
. These properties are also subtypes of
ObservableValue
. So they
can also be used as source objects for binding properties.
By convention, each binding property (e.g.,
centerX
) in a JavaFX class (e.g.,
Circle
) has a
getter (e.g.,
getCenterX()
) and setter (e.g.,
setCenterX(double)
) method for returning and
setting the property's value. It also has a getter method for returning the property itself. The naming
convention for this method is the property name followed by the word
Property
. For example,
the property getter method for
centerX
is
centerXProperty()
. We call the
getCenterX()
method as the
value getter method
, the
setCenterX(double)
method as the
value setter method
,
and
centerXProperty()
as the
property getter method
. Note that
getCenterX()
returns
a
double
value and
centerXProperty()
returns an object of the
DoubleProperty
type.
FigureĀ 14.7a shows the convention for defining a binding property in a class and FigureĀ 14.7b
shows a concrete example in which
centerX
is a binding property of the type
DoubleProperty
.
the
Property
interface
the
ObservableValue
interface
common binding properties
common
ObservableValue
objects
value getter method
value setter method
property getter method
public class
SomeClassName {
public class
Circle {
private
PropertyType x;
private
DoubleProperty centerX;
/** Value getter method */
public
propertyValueType getX() { ... }
/** Value getter method */
public double
getCenterX() { ... }
/** Value setter method */
public void
setX(propertyValueType value) { ... }
/** Value setter method */
public void
setCenterX(
double
value) { ... }
/** Property getter method */
public
PropertyType
xProperty() { ... }
}
/** Property getter method */
public
DoubleProperty centerXProperty() { ... }
}
(b)
centerX
is binding property
(a)
x
is a binding property
F
IGURE
14.7
A binding property has a value getter method, setter method, and property getter method.
The program in Listing 14.5 is the same as in Listing 14.4 except that it binds
circle
's
centerX
and
centerY
properties to half of
pane
's width and height (lines 16-17). Note that
circle.centerXProperty()
returns
centerX
and
pane.widthProperty()
returns
width
. Both
centerX
and
width
are binding properties of the
DoubleProperty
type. The
numeric binding property classes such as
DoubleProperty
and
IntegerProperty
con-
tain the
add
,
subtract
,
multiply
, and
divide
methods for adding, subtracting, multiply-
ing, and dividing a value in a binding property and returning a new observable property. So,
pane.widthProperty().divide(2)
returns a new observable property that represents
half of the
pane
's width. The statement
circle.centerXProperty().bind(pane.widthProperty().divide(
2
));
is same as
centerX.bind(width.divide(
2
));
Search WWH ::
Custom Search