0

i'm struggling with a vertical box i added a transition to, it stays at the back of the other nodes like label and imageview. i tried using toFront() but it didn't work. also i tried using .setStyle("-fx-view-order: [number]") but i was lost a bit in what the numbers represent exactly is it the order of nodes ascending or descending.


#1

vb.setStyle("-fx-view-order: 0");

img.setStyle("-fx-view-order: 1");

didn't work

#2

vb.setStyle("-fx-view-order: 1");

img.setStyle("-fx-view-order: 0");

didn't work

1 Answers1

2

The easiest way to get a Node to render on top is to move it to the end of the child node list for its parent Node.

Note the documentation for -fx-view-order mentions: "The parent traverses its children in decreasing viewOrder order." To me that means higher view order is behind (drawn first) relative to lower view order. However, this is all relative to the parent node, not the Scene. Give us a reproducible example of what you are doing.

Also, what version of JavaFX are you using? -fx-view-order was not in JavaFX 8 (JDK 8) https://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html#node

swpalmer
  • 3,890
  • 2
  • 23
  • 31
  • "_The easiest way to get a Node to render on top is to move it to the end of the child node list for its parent Node_" – That depends. If your nodes are in a layout which positions them based on their order in the children list, changing the order of the list can have undesired effects. Also, note that JavaFX 2.X was during the Java 7 era. The version number of Java and JavaFX have been mirroring each other since Java 8. – Slaw Dec 05 '21 at 18:54
  • right!! that worked. thank you i didn't think of putting it at the end of the child nodes list. i'm using jdk 8 so maybe thats why it didn't work with me – user9910282 Dec 05 '21 at 21:02
  • @user9910282 If this answer solved your issue, you should mark it correct. – jewelsea Dec 05 '21 at 22:14
  • @Slaw Yes, right about the FX version. I've updated the answer. You are also right about potential side-effects of moving the child node in the list, however in most cases when that matters the layout would avoid overlapping nodes as well, so it isn't likely to apply. – swpalmer Dec 05 '21 at 23:20
  • 1
    That's true, regarding layouts typically not overlapping their children, but all bets are off once you start transforming the child nodes. For instance, maybe you want to do [something like this](https://stackoverflow.com/questions/51071095/javafx-css-move-node-to-front/51071882#51071882). In that example, changing the order of the children would mess everything up (or at least make the implementation much more difficult). I only bring this up because the OP mentioned "transition". – Slaw Dec 06 '21 at 01:02
  • Where is the documentation for `-fx-view-order`, could you link to it? – jewelsea Dec 06 '21 at 03:38
  • 1
    @jewelsea The CSS property is documented [here](https://openjfx.io/javadoc/17/javafx.graphics/javafx/scene/doc-files/cssref.html#node). And the corresponding JavaFX property is documented [here](https://openjfx.io/javadoc/17/javafx.graphics/javafx/scene/Node.html#viewOrderProperty()). – Slaw Dec 06 '21 at 09:21