1
votes

I have a tree like this:

enter image description here

As you can see there is GridPane with 10 columns. Each of them include BorderPane wrapped into AnchorPane. Each BorderPane is composed of 2 labels and 1 radioButton. Below you can see how it look like:

enter image description here

I want to ask you how to get acces from code side to these elements. I know that I can use getChildren() method on GridPane but then I get only AnchorPanes from GridPane columns. I want to go deeply, and for example set text into one of the labels.

I want to add that set id in Scene Builder is not what I want cause there will be many columns and I'm going to fill it in some loop.

Could you help me with this ?

One more thing: I build the view in Scene Builder.

1
It is so easy to vote down instead help.Koin Arab

1 Answers

2
votes

You you can set a css id for the nodes within your loop at creation time. You can later lookup the nodes by the id you set. A lookup will also work on a scene to find any node or matching set of nodes within the scene.

HBox parent = new HBox();
for (int i = 0; i < N_COLS, i++) {
    Node childNode = createNode();
    childNode.setId("child" + i);
    parent.getChildren().add(childNode);
}
. . .
Node redheadedStepchild = parent.lookup("#child5");

Of course, you can always access the child the standard object accessor way too:

Node redheadedStepchild = parent.getChildren().get(5);

If the structure is nested as you have in your question, then you can do things such as:

Node redheadedGrandchild = grandparent.getChildren().get(3).getChildren().get(5);

Such searches get convoluted over large structures and probably are best avoided as your code would get more brittle. So a css based lookup or tree traversal would probably be preferred, or a direct injection of a reference via an fx:id and @FXML, which wouldn't work well in your case because you create the items in code in a loop, hence the lookup method is probably best for you.