Java Reference
In-Depth Information
(a)
(b)
F
IGURE
14.16
The nodes fill in the rows in the
FlowPane
one after another.
The program creates a
FlowPane
(line 13) and sets its
padding
property with an
Insets
object (line 14). An
Insets
object specifies the size of the border of a pane. The constructor
Insets(11, 12, 13, 14)
creates an
Insets
with the border sizes for top (11), right (12),
bottom (13), and left (14) in pixels, as shown in Figure 14.17. You can also use the constructor
Insets(value)
to create an
Insets
with the same value for all four sides. The
hGap
and
vGap
properties are in lines 15-16 to specify the horizontal gap and vertical gap between two
nodes in the pane, as shown in Figure 14.17.
hGap
Top side
Pane
vGap
Right side
Left side
Bottom side
F
IGURE
14.17
You can specify
hGap
and
vGap
between the nodes in a
FlowLPane
.
Each
FlowPane
contains an object of
ObservableList
for holding the nodes. This list
can be obtained using the
getChildren()
method (line 19). To add a node into a
FlowPane
is to add it to this list using the
add(node)
or
addAll(node1, node2, ...)
method.
You can also remove a node from the list using the
remove(node)
method or use the
removeAll()
method to remove all nodes from the pane. The program adds the labels and
text fields into the pane (lines 19-24). Invoking
tfMi.setPrefColumnCount(1)
sets the
preferred column count to
1
for the MI text field (line 22). The program declares an explicit
reference
tfMi
for a
TextField
object for MI. The explicit reference is necessary, because
we need to reference the object directly to set its
prefColumnCount
property.
The program adds the pane to the scene (line 27), sets the scene in the stage (line 29),
and displays the stage (line 30). Note that if you resize the window, the nodes are auto-
matically rearranged to fit in the pane. In Figure 14.16a, the first row has three nodes, but in
Figure 14.16b, the first row has four nodes, because the width has been increased.
Suppose you wish to add the object
tfMi
to a pane ten times; will ten text fields appear in
the pane? No, a node such as a text field can be added to only one pane and once. Adding a
node to a pane multiple times or to different panes will cause a runtime error.
Note
A node can be placed only in one pane. Therefore, the relationship between a
pane and a node is the composition denoted by a filled diamond, as shown in
Figure 14.3b.
Search WWH ::
Custom Search