Issue
I'm new to both java and javafx. How can I check if any object like Rectangle or Button exists in the Stack Pane or in the scene ? Tried searching in google but couldn't find anything related to the same.
Solution
To check if a Node
(could be Button
, Rectangle
or any other UI node) is a direct child of another node (parent assumed, StackPane
in your question) you can do the following:
stackPane = ...
if (stackPane.getChildren().contains(node)) {
// node is a direct child of stackPane
}
Alternatively, you can call node.getParent()
to obtain a reference to the parent node, if there is one.
Finally, by calling node.getScene() != null
you can check if a node is part of a scene.
For full documentation refer to JavaFX API.
Answered By - AlmasB