How to create custom dialog with FXML in JavaFX?


Question

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?

1
2
5/14/2014 8:27:40 AM

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.

1
5/14/2014 8:29:25 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon