Java Reference
In-Depth Information
two elements (such as two buttons) would be positioned in such a way that no space was
between them. Thus, the controls would run together, creating a very unappealing user in-
terface. Specifying gaps prevents this.
The second point of interest is the following line, which sets the alignment of the ele-
ments in the FlowPane :
Here, the alignment of the elements is centered. This is done by calling setAlignment( ) on
the FlowPane . The value Pos.CENTER specifies that both a vertical and horizontal center
will be used. Other alignments are possible. Pos is an enumeration that specifies alignment
constants. It is packaged in javafx.geometry .
Before moving on, one more point needs to be made. The preceding program used an-
onymous inner classes to handle button events. However, because the EventHandler inter-
face defines only one abstract method, handle( ) , a lambda expression could have passed
to setOnAction( ) , instead. For example, here is the handler for the Up button, rewritten to
use a lambda:
Notice that the lambda expression is more compact than the anonymous inner class. (You
will use lambda expressions when you modify this example as part of exercise 10 in the
Self Test.)
Three More JavaFX Controls
JavaFX defines a rich set of controls, which are packaged in javafx.scene.control . You
have already seen two of them: Label and Button . Here, we will look at three more:
CheckBox , ListView , and TextField . As their names imply, they support a check box, a list
control, and a text field. Combined, these provide a representative sampling of the JavaFX
controls. They also help demonstrate several common techniques. Once you understand the
basics, you will be able to explore the other controls on your own.
The controls described here provide functionality similar to that of the Swing controls
presented by the preceding Swing chapter. As you work through this section, you might
find it interesting to compare the way these controls are implemented by the two frame-
works.
CheckBox
Search WWH ::




Custom Search