Damian
Damian

Reputation: 461

Reference a node from FXML file

I have a very simple question. When I create my user interface in Scene Builder, I would like to reference it later on in my code. So for example, I would create a pane in my FXML, load it to my scene, then put that scene in my stage. After I would like to do something like get this pane by ID or any kind of reference and add some elements to it, for example after the button was clicked I would add a picture to this referenced pane. Also, I would do that from my controller (onclick of the button that was created in my fxml) so, do I need to have some sort of reference to my scene or having some kind of method that will manipulate content of that pane? . Is it possible?

Upvotes: 2

Views: 2094

Answers (1)

Perneel
Perneel

Reputation: 3367

In your controller, add the following:

@FXML
private Pane p

Save it first.

Afterwards, through Scene Builder you can add a fx:id to your Pane. Just select the Pane, and choose the 'p' in the dropdown list. Or you can do it directly in FXML:

<Pane fx:id="p" ....></Pane>

To add elements to pane 'p' when you hit a button or such, use this:

p.getChildren().add(...)

Upvotes: 9

Related Questions