Java Reference
In-Depth Information
This provides the basic JavaFX code, but these instance values are still not con-
nected at all to the
JSpinner
object. To start connecting these instance variables
to JavaBeans properties, you assign a default value from the
SpinnerModel
and
then when the instance variable changes, you set that value in the
JSpinner
object. Here is how
value
is now set.
public var value:Integer =
getModel().getNumber().intValue() on replace {
getModel().setValue(value);
};
There are a couple of things to point out with this. First,
getModel()
is a conve-
nience function that just gets the
SpinnerNumberModel
object from the
JSpinner
.
function getModel() : SpinnerNumberModel {
getJSpinner().getModel() as SpinnerNumberModel;
}
Second, it is necessary to narrow the model's
getNumber()
result. The
getNumber()
method returns a
java.lang.Number
and this needs to be coerced to an integer
by calling its
intValue()
method. The rest of the variables work similarly; how-
ever, the variables
minimum
and
maximum
have a little twist.
In the
SpinnerNumberModel
, the
minimum
and
maximum
properties return an
object that may be null. When they are null, there is no minimum or maximum
integer, in effect they are infinite. The issue with this is that currently, Number
and Integer in JavaFX cannot be null, so we have to deal with this. There are sev-
eral options for handling this. One is to create a flag, one for each of the two
instance variables, to indicate that they are infinite. The other is to convert the
null to a very low minimum or a very high maximum, respectively. We decided
to do the latter. For the
minimum
variable, we created the
getMinimum()
function
as shown in the following listing.
function getMinimum()
: Integer {
if(getModel().getMinimum() == null) {
java.lang.Integer.MIN_VALUE;
}else {
(getModel().getMinimum() as java.lang.Number).
intValue();
}
}
If the
SpinnerNumberModel
minimum property is null, this function returns
java.lang.Integer.MIN_VALUE
. Next, we modify the
minimum
instance vari-
able to be initialized using this function.
Search WWH ::
Custom Search