Java Reference
In-Depth Information
Table 5-9. Properties Available on Nodes in a GridPane Layout
Name
Type
Description
halignment
Horizontal alignment of nodes in GridPane cell; can be one of LEFT, CENTER,
or RIGHT
HPos
valignment
Vertical alignment of nodes in a GridPane cell; can be one of TOP, CENTER,
BASELINE, or BOTTOM
VPos
hgrow
Priority for the GridPane cell growing horizontally beyond its preferred side
Priority
vgrow
Priority for the GridPane cell growing vertically beyond its preferred size
Priority
margin
Margin surrounding the element in the GridPane cell, specified as an Inset with
a top, left, bottom, and right stand-off
Insets
fillWidth
Boolean
Specifies if the node should be expanded to fit the column width, or kept at its
own width
fillHeight
Boolean
Specifies if the node should be expanded to fit the row height, or kept at its
own height
Just as with earlier layouts, these constraints can be set via a series of like-named static methods on the GridPane
class that accept a node and constraint value. The first two constraints for alignment are similar to the alignment on
StackPane , except they are constrained to be in either the horizontal or vertical direction. However, the cross-product
of using horizontal and vertical alignment together gives you the same 12 combinations.
Margin is similar to the like-name constraint first described in the earlier section entitled, “Aligning to Edges
Using StackPanes and TilePanes .” It is also the only constraint that can be applied solely to individual nodes, but not
an entire row or column.
Both hgrow and vgrow take a value of type Priority , similar to the like-name constraints on HBox and VBox ,
respectively. These can be given one of three possible values:
NEVER: Will never grow beyond the preferred dimensions of the node.
grow priority
SOMETIMES: Will only grow if there is still available space after all nodes with
“ALWAYS” are taken into account.
ALWAYS: Will grow up to the maximum dimensions of the node, sharing space equally with
other nodes that have a grow priority of “ALWAYS.”
Setting the grow constraint property can greatly simplify the job of laying out complex UIs. For example, you
could use a grow constraint of NEVER on a button and ALWAYS on a TextField to ensure that the text field fills the
form width, and the button is sized perfectly for the contained text.
The fillWidth and fillHeight properties are useful for nodes with preferred sizes that are smaller than the
allocated space in the GridPane . If those properties are supplied with a Boolean value of true, the container will rather
use the available size instead of the preferred size to layout the node.
We take advantage of the flexible capabilities of the GridPane to lay out the grid of squares and pieces for the
Reversi application. The default alignment , grow policy, and margin work perfectly for the needs of our playing board,
so the only thing we need to update as we add in squares is the x and y position. We don't even need to set the size of
the grid, because it will automatically scale to fit the number and position of components that get added in.
To start with, we need to update the start method in the Reversi application to put the tiles on top of the
background. To accomplish this we make use of a StackPane to compose the center region of the BorderLayout as
shown in Listing 5-14.
 
Search WWH ::




Custom Search