JavaFX-setVisible 隐藏元素,但不重新排列相邻节点

在 JavaFX 中,如果我有一个包含2个 VBox元素的场景,并且每个 VBox都包含多个 Label
如果我把上面的 VBox设置成 隐形的,为什么下面的 VBox和上面的 VBox一样?

VBox隐形的,但我希望其他对象移动到它的位置。

我正在使用 FXML 加载我的控件。

81684 次浏览

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.

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

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());

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.

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

myHBox.setVisible(false);
myHBox.managedProperty().bind(myHBox.visibleProperty());