How do I set Z-order for VBox
in JavaFX?
Z-order in JavaFX is actually the order in the scenegraph, eg. in the content sequence of the scene (then in the contents of the groups, containers, etc.).
All nodes have also toFront() and toBack() functions to help changing this order. For finer control, you have to remove nodes from one place and insert it higher or lower in the sequence.
With the toFront() and toBack() functions you can indeed influence the z-order, but be aware that this also influences the layout. The HBox and VBox for instance also use the sequence of children to do the layout and moving something to the front will also move it to the end of the [HV]Box. This might not be what you are looking for.
I was looking for a way to do an animation with the animated Node on top of all others, without messing up the layout. There seems to be no way to do that because the z-order and layout order are both taken from the child-order.
While the other answers are still correct, JavaFX 9 added a way to manipulate the Z-order without modifying the children list. This is done with the Node.viewOrder
property.
Javadoc:
Defines the rendering and picking order of this
Node
within its parent.This property is used to alter the rendering and picking order of a node within its parent without reordering the parent's
children
list. For example, this can be used as a more efficient way to implement transparency sorting. To do this, an application can assign theviewOrder
value of each node to the computed distance between that node and the viewer.The parent will traverse its
children
in decreasingviewOrder
order. This means that a child with a lowerviewOrder
will be in front of a child with a higherviewOrder
. If two children have the sameviewOrder
, the parent will traverse them in the order they appear in the parent'schildren
list.However,
viewOrder
does not alter the layout and focus traversal order of thisNode
within its parent. A parent always traverses itschildren
list in order when doing layout or focus traversal.Default value:
0.0
Since:
9
来源:https://stackoverflow.com/questions/2988196/z-order-in-javafx