When it comes to minimum, preferred, and maximum dimensions of a Region
, there are three different modes1:
- The developer sets them explicitly (e.g.
setPrefWidth(400)
).
- They are set to
Region.USE_COMPUTED_SIZE
(i.e. -1.0
)2.
- They are set to
Region.USE_PREF_SIZE
(i.e. negative infinity)1.
1. The preferred dimensions only have two modes as USE_PREF_SIZE
only has meaning for the minimum and maximum dimensions.
2. This is the default for Region
and most (all?) of its subclasses.
When the dimensions are set to USE_COMPUTED_SIZE
this means the values are computed during layout, using protected methods such as:
And similar methods for the minimum and maximum dimensions.
When it comes to Control
s, however, these computations are typically delegated to the skin class. Most skin implementations inherit from SkinBase
which provides similar methods for computing min/pref/max dimensions; they have the same names but accept more parameters to make the computations a little easier. You note in a comment that, in the case of TableView
, the computations for preferred width and height take place in TableViewSkinBase
.
You may be wondering how you can get this computed value; I don't believe you can in a reliable fashion. You could try using the public methods of Node
, such as Node.prefWidth(double)
, but the problem is knowing what value to pass as an argument. In the case the value shouldn't be -1
then it's really only the parent that knows for certain how much space is available.
Note: These methods of Node
(e.g. prefWidth(double)
) are the "entry point" for getting the minimum, preferred, and maximum dimensions of a Node
. However, the min
, pref
, and max
properties are not universal (available for Region
s and, to an extent, WebView
) and the computeXXX
methods are unique to Parent
(and its subclasses). Other kinds of Node
s, such as Shape
s, provide their own internal mechanisms.
Also, the preferred dimensions are just a guideline. The parent is free to size its children as it sees fit within the constraints of the min and max values. If you want to know what the dimensions of a node ultimately end up being you can use the Region.width
and Region.height
properties (when using a Region
) or the Node.boundsInLocal
property (for an arbitrary Node
). WebView
and some Shape
implementations also provide properties for knowing their dimensions.
Note:
This answer focuses on Node
s (i.e. the scene graph). As pointed out by kleopatra, other objects (e.g. TableColumnBase
) that have properties like prefWidth
don't have the same specified behavior as Region
. For instance, setting the value to -1
is not defined to behave the same way. This also applies to WebView
which, while having min/pref/max dimension properties, is not a Region
.
The TableView
's default skin implementation takes into account the prefWidth
of each visible TableColumn
when computing its preferred width.