Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference a node from FXML file

Tags:

fxml

javafx-2

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?

like image 955
Damian Avatar asked Mar 03 '26 15:03

Damian


1 Answers

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(...)
like image 168
Perneel Avatar answered Mar 05 '26 10:03

Perneel