JavaFX - setVisible hides the element but doesn't rearrange adjacent nodes
Asked Answered
C

5

105

In JavaFX, if I have a scene with 2 VBox elements and each VBox has multiple Label in it.
If I set the top VBox to invisible, why does the bottom VBox not move up the scene where the top VBox was ?

The VBox is invisible but I would expect the other objects to move into its place.

I am using FXML to load my controls.

Cantle answered 17/2, 2015 at 9:5 Comment(2)
Because the first VBox is just invisible and not removed from its parent.Hurlburt
whether or not hidden nodes take up any space typically is the decision of the layoutSestos
L
203

Node.setVisible(boolean) just toggles the visibility state of a Node.

To exclude a Node from its parents layout calculations you additionally have to set its managed state, by calling Node.setManaged(false).

If you want the managed state to be updated automatically alongside the visibility, you can use a binding as @jewelsea pointed out: node.managedProperty().bind(node.visibleProperty());

Lala answered 17/2, 2015 at 10:35 Comment(1)
A binding also works, e.g., node.managedProperty().bind(node.visibleProperty());Rotten
S
7

Since it's invisible, it wont move to the top. You have to remove it with something like:

// remove
vbox.getChildren().remove(...)

Once you've removed the element you want invisible then, the other element should move to the top.

Scuttlebutt answered 17/2, 2015 at 9:12 Comment(1)
AFAIK, manipulating the scene graph is more expensive than setting visible to false.Domineer
P
5

Try to use setVisible and managedProperty together. Here is an example:

myHBox.setVisible(false);
myHBox.managedProperty().bind(myHBox.visibleProperty());
Planer answered 11/11, 2019 at 11:54 Comment(2)
nothing new compared to the answer with the most upvotes, is there :)Sestos
more clearer now!Fanlight
K
3

Instead of hiding the vbox you should remove it from the Children and if you want to show it again add the vbox again.

Krissykrista answered 17/2, 2015 at 9:13 Comment(0)
E
-1

If l want to hide and unhide a node, I resize the node to 0 if l want to hide it. That way, the node will not occupy space since is not visible to the user, so when l want it to be visible, l adjust the size again for it to be visible.

Egidius answered 15/10, 2019 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.