Suzan Cioc
Suzan Cioc

Reputation: 30127

How to create custom dialog with FXML in JavaFX?

How to create custom dialog with FXML in JavaFX?

In samples over the Net I see mostly something like this

@Override
public void start(Stage stage) throws Exception {
   Parent root =
      FXMLLoader.load(
         getClass().getResource( getClass().getSimpleName() + ".fxml" ));
   Scene scene = new Scene(root);

i.e. FXML is loaded from within application start() and builds root node.

But what if I extend Stage? Where to load from FXML? In constructor? Or in initStyle()? Or in some other method?

Upvotes: 2

Views: 7945

Answers (1)

Jitendra Pareek
Jitendra Pareek

Reputation: 637

You may use the below code in your main Class :

FXMLLoader loader = new FXMLLoader(getClass().getResource("Sample.fxml"));
Parent root = (Parent)loader.load();

//Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));        
Scene scene = new Scene(root);        
stage.setScene(scene);
stage.show();

SampleController controller = (SampleController)loader.getController();
controller.setStageAndSetupListeners(stage); 

After this in SampleController Make a function setStageAndSetupListeners(), which will accept your stage and now you use it easily.

Upvotes: 1

Related Questions