Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX - How to delete a specific Node from an AnchorPane

I'm using SceneBuilder 8.0.0 and JavaFX 8.
I have a Button btn and a Label lbl attached to an AnchorPane ap.
When the application starts, btn and lbl are attached to ap.

How can i delete one of these nodes ? (i only know clear() method which deletes all the nodes from ap). thanks.

like image 397
Mohamed Benmahdjoub Avatar asked May 03 '15 19:05

Mohamed Benmahdjoub


People also ask

How do I remove a node from GridPane?

For me the best, and the shortest way is: gridPane. getChildren(). removeIf( node -> GridPane.

How do I remove a circle in Javafx?

Add circle with ALT + Click , and remove them by simply clicking on them.

What is an anchor pane in JavaFX?

The Anchor Pane allows the edges of child nodes to be anchored to an offset from the anchor pane's edges. If the anchor pane has a border and/or padding set, the offsets will be measured from the inside edge of those insets.

How do I delete a button in Javafx?

You just need to call projectList. getChildren(). remove(button); in the button's handler method. (As an aside, you should really use setOnAction , not setOnMouseClicked .)


1 Answers

In JavaFX, nodes can simply be removed from a Parent (e.g. an AnchorPane) using .getChildren() following by .remove(Object o)

Reference

So if you have a direct reference to these Nodes, you could use the following code to remove the Button from the AnchorPane:

ap.getChildren().remove(btn);

Lookup

If you, for some reason, don't have a reference to the Button btn you can use lookup(String selector) to find and remove it like so:

ap.getChildren().remove(ap.lookup('.button'));

FXML

Or finally, since you are using SceneBuilder (and thus fxml) you could also make sure you hava a Controller connected and assign an id to your Button to get ahold of a reference and remove it like so:

// ... somewhere in your class
@FXML
private Button myButtonId;

// ... somewhere in a method
ap.getChildren().remove(myButtonId);
like image 54
Mike Rombout Avatar answered Oct 23 '22 23:10

Mike Rombout