-1

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.

FARS
  • 313
  • 6
  • 20
  • 4
    [mcve] please .. – kleopatra Sep 08 '21 at 08:50
  • 1
    For help with your code you need to post mre as requested. Also see https://stackoverflow.com/questions/38101033/how-to-obtain-access-to-children-nodes-in-javafx?rq=1 – c0der Sep 08 '21 at 12:54
  • @kleopatra, could you highlight which improvement you consider to be necessary? I've highlighted my attempts and shared the results they generated. I agree that my post looks verbose, but it felt necessary to describe the problem properly. About it being replicable, my system has way too many connections and references to another classes' objects, there was no way to post the original code here since it wouldn't help with the explanation and understanding. Thanks for the suggestion though – FARS Sep 08 '21 at 17:00
  • "If a program adds a child node to a `Parent`…the node is automatically (and _silently_) removed from its former parent." Can you use the `id` property to let the pane identify itself? – trashgod Sep 08 '21 at 17:12
  • @trashgod yep that's possible, since the my arraylist prints something like that: [AnchorPane[id=anchorPane], JFXButton[id=button], etc etc etc] Although I have no idea how to use these values. I've tried with node.getId().equals(node) but that doen'st worked well – FARS Sep 08 '21 at 19:02
  • 2
    @FARS "could you highlight which improvement you consider to be necessary?" Read the link kleopatra provided. What was being asked was to provide complete, minimal code to reproduce the issue (and nothing else), just by doing a copy+paste+compile+run. For example, you could take the code from Sai's answer without change and run it, but not so for code in your original question. – jewelsea Sep 08 '21 at 20:53
  • @FARS: Those values are explained in [_How do I print my Java object without getting "SomeType@2f92e0f4"?_](https://stackoverflow.com/q/29140402/230513) – trashgod Sep 08 '21 at 22:28

1 Answers1

1

On high level your code looks fine. Having said that there are some considerable changes/cleanup you need to do.

  • no need of static ArrayList
  • no need to check in a for loop. ...

I quickly tried your code with some changes and it is working fine. Try modifying accordingly. Even if you have issues.. then you might be doing something wrong elsewhere.

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

public class LayoutDemo extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        AnchorPane anchorPane2 = new AnchorPane(new StackPane());
        AnchorPane anchorPane3 = new AnchorPane(new StackPane());

        // Change the internal node to anchorPane3 to get "not here" output
        AnchorPane mainAnchorPane = new AnchorPane(new StackPane(new Pane(new VBox(anchorPane2, new Pane()), new Pane())), new Pane());
        Pane root = new Pane(mainAnchorPane);
        Scene scene = new Scene(root, 400, 400);
        stage.setScene(scene);
        stage.show();

        if (getAllNodes(mainAnchorPane).contains(anchorPane2)) {
            System.out.println("ap2 is here");
        } else {
            System.out.println("ap2 is not here");
        }
    }

    public static List<Node> getAllNodes(Parent root) {
        List<Node> nodes = new ArrayList<Node>();
        addAllDescendents(root, nodes);
        return nodes;
    }

    private static void addAllDescendents(Parent parent, List<Node> nodes) {
        for (Node node : parent.getChildrenUnmodifiable()) {
            nodes.add(node);
            if (node instanceof Parent)
                addAllDescendents((Parent) node, nodes);
        }
    }
}

Sai Dandem
  • 8,229
  • 11
  • 26