I have an AnchorPane which has a considerable amount of containers inside of it (most of them are AnchorPanes as well): MainAnchorPane is always set as the top anchorPane and it has multiple anchorPane as children(let's say these are AnchorPane2 and AnchorPane3).
Looking forward to finding a way to identify which anchorPane is inside of the main container, I've tried to do the following:
if (mainAnchorPane.getChildren().contains(anchorPane2))
{ System.out.println("anchorPane2 is opened here"); }
else if (mainAnchorPane.getChildren().contains(anchorPane3))
{System.out.println("anchorPane3 is opened here");}
else {System.out.println("no anchorPane found");}
Although, this method does not work.
I also tried to compare anchorPanes from another classes, but that gives me NullPointerException (even with that anchorPane being open is another part of the system -For instance, I have multiple MainAnchorPanes which can handle the same anchorPane2 and 3 to be open at the same time-):
if (mainAnchorPane.getChildren().contains(anotherClass.anchorPane2))
{System.out.println("anchorPane2 is opened here");}
else {System.out.println("no anchorPane found");}
That would be my preferred method, but as I mentioned, for some reason it gives me a null exception in the life of the if statement.
Finally, I have tried the last effort to identify which anchorPane is currently being shown in my MainAnchorPane by creating a list of nodes inside the mainAnchorPane and comparing each of them with the target node, which is anchorPane2 or anchorPane3:
for (Node node: getAllNodes(mainAnchorPane))
{
System.out.println(node);
// in the place of equals() I've tried contains() as well
if (node.equals(anchorPane2)) {
System.out.println("ap2 is here");
}
else {System.out.println("ap2 is not here");}
}
public static ArrayList<Node> nodes = new ArrayList<Node>();
public static ArrayList<Node> getAllNodes(Parent root) {
addAllDescendents(root, nodes);
return nodes;
}
private static void addAllDescendents(Parent parent, ArrayList<Node> nodes) {
for (Node node : parent.getChildrenUnmodifiable()) {
nodes.add(node);
if (node instanceof Parent)
addAllDescendents((Parent)node, nodes);
}
}
None of these methods worked. I even went further and checked whether my ifs catch buttons and labels nodes inside of AnchorPanes which are nodes of that pane, although the condition always returns as false, suggesting the node is not in there (which is not true, since if shows up in all in System.out.println(nodes, mainAnchorPane.getChildren(), and getAllNodes(mainAnchorPane)))
Any thoughts would be appreciated.
Edit: it should be as simple as
if (mainAnchorPane.getChildren().contains(node))
but that doesn't work as well
Naming convention were simplified aiming for better explanation.