Z-Order in JavaFX

2019-01-26 06:00发布

How do I set Z-order for VBox in JavaFX?

3条回答
爷的心禁止访问
2楼-- · 2019-01-26 06:10

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 the viewOrder value of each node to the computed distance between that node and the viewer.

The parent will traverse its children in decreasing viewOrder order. This means that a child with a lower viewOrder will be in front of a child with a higher viewOrder. If two children have the same viewOrder, the parent will traverse them in the order they appear in the parent's children list.

However, viewOrder does not alter the layout and focus traversal order of this Node within its parent. A parent always traverses its children list in order when doing layout or focus traversal.

Default value:

0.0

Since:

9

查看更多
在下西门庆
3楼-- · 2019-01-26 06:19

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.

查看更多
smile是对你的礼貌
4楼-- · 2019-01-26 06:30

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.

查看更多
登录 后发表回答