Java Reference
In-Depth Information
Because the set of interesting layout constraints is fairly small, the ScalaFX APIs simply add the common ones
onto the Node class directly. This lets you specify constraints such as alignment, margin, and grow declaratively as you
are creating your object tree.
For example, to add a margin to the Label in our Hello ScalaFX example, it is as easy as setting the margin
property on the Label :
object HelloScalaFXMargin extends JFXApp {
stage = new PrimaryStage {
scene = new Scene {
content = new Label {
text = "Hello ScalaFX Margin"
margin = new Insets(20)
}
}
}
}
This adds 20 pixels around the Label to space it out in the window. Without this ScalaFX feature, you would have
been required to save a reference to the Label and later call the following.
StackPane.setMargin(new Insets(20));
An added benefit of the ScalaFX Node Layout Constraints is that they apply across all JavaFX layouts regardless of
which type of container you are using. With the normal JavaFX Layout Constraints, you need to use the static method
from the right layout type, otherwise the constraint will not work.
For layout authors who want to make use of the scalaFX node Layout Constraints, scalaFX also stores the
layout constraint in an unprefixed form that you can directly access. For example, to get the margin constraint, simply
call node.getProperties().get("margin") . For alignment you get your choice of accessing it as “alignment,”
“halignment,” or “valignment,” all of which get updated anytime the user sets the alignment property.
Tip
Animation
ScalaFX provides a shortcut syntax for expressing timelines that was inspired by the same syntax in JavaFX Script. It
lets you specify the duration, keyframes, keyvalues, and tweens in a shortcut syntax that fits on a single line. We used
this earlier in the Vanishing Circles example to specify an animation that shrinks the circles when they are clicked:
Timeline(at (3 s) {radius -> 0}).play()
This code is equivalent to the following Java code using the JavaFX animation API.
KeyValue collapse = new KeyValue(circle.radiusProperty(), 0);
new Timeline(new KeyFrame(Duration.seconds(3), collapse)).play();
 
 
Search WWH ::




Custom Search