Java Reference
In-Depth Information
translateY: bind (scene.height -
rect.layoutBounds.height)/2,0 -
rect.layoutBounds.minY
...
}
}
}
Notice that we need to assign local variables to both the Scene and the Rectangle,
and then use those variables within a bind to set the
translateX
and
translateY
variables. Also, notice that we need to include the
minX/minY
for each, as this
would include any offset due to shape drawing, effects, and clipping.
If you need more control over layouts, you need to create a custom layout using
the
javafx.scene.layout.Container
class. To illustrate this, we will walk
through a
GridLayout
class. The
GridLayout
class lays out its nodes in a series
of rows and columns. Instead of using a fixed width and height for the rows and
columns, this layout class calculates the width of each column to be wide enough
to handle the widest node within the column. Likewise, the height for each row is
calculated to handle the maximum height to accommodate each node in that row.
Therefore, the column widths and row heights vary depending on the nodes con-
tained in each.
Because some nodes will have extra horizontal or vertical space within a row or
column, you can specify the alignment of those nodes within the row or column.
Either align left, right, or center for columns, and align top, center, or bottom
within rows. The actual alignment values are directional with values for North,
NorthEast, East, and so on.
To begin, we create the
GridLayout
class extending
javafx.scene.layout
.Container
.
public class GridLayout extends Container {
javafx.scene.layout.Container
extends
javafx.scene.Group
, so from there
we have access to the
content[]
sequence of Nodes. Also,
Container
extends
javafx.scene.layout.Resizable
, and from there we have access to
width
,
height
,
minimum width
/
height
,
maximum width
/
height
, and
preferred width
/
height
. There are also some helper functions defined in
Resizable
; we will use
getNodePreferredWidth/Height()
.
Search WWH ::
Custom Search