Ciel
Ciel

Reputation: 6281

How can I add components into fxml scene using code

I am using JavaFX and a scene is loaded by fxml file.

FXMLLoader loader = new FXMLLoader();
Parent rootNode = (Parent) loader.load(fxmlFile);
setScene(new Scene(rootNode));
stage.setScene(scene);

How can I add components into this scene? For example How can I add a button using code into this scene?

Upvotes: 1

Views: 1020

Answers (1)

ItachiUchiha
ItachiUchiha

Reputation: 36722

Lets consider you have borderPane as the root element of the FXML. Now you can go on as this :

FXMLLoader loader = new FXMLLoader();
Parent rootNode = (Parent) loader.load(fxmlFile);
Button button = new Button();
((BorderPane) rootNode).setCenter(button);
setScene(new Scene(rootNode));
stage.setScene(scene);

Upvotes: 1

Related Questions