JavaFX remove objects from AnchorPane
I have
Root AnchorPane
HBox at the root
two VBoxes in an HBox, each unnamed, a new AnchorPane and an unnamed label in each AnchorPane with text
There is a table, but the template just doesn't suit me.
When I need to create a new table on the same spot table with new content, I do this:
root.clearConstraints(hBox);
hBox = new HBox();
root.getChildren().add(hBox);
and re-create the table. But the sad thing is that the previous content from root remains. How can I remove this?
source to share
To remove elements from Pane
you need to remove them from your children. You have several options:
root.getChildren().remove(hBox); // remove a single item
root.getChildren().removeAll(box1, box2, box3); // remove all listed items (varargs)
root.getChildren().removeAll(collectionOfNodes); // remove all items in a Collection
root.getChildren().clear(); // remove all children
There are others: getChildren()
returns ObservableList<Node>
, which extends List<Node>
, so you have access to all of these methods.
source to share