Search code examples
javafxtextfieldsetvalue

JavaFX: Set textfield value before showing window


I'm creating a new modal dialog from the main controller class. How do I set some textfield values in the dialog before it is displayed?

URL url = getClass().getResource("SeedNodeForm.fxml");
Stage stage = new Stage();
stage.setTitle("Seed Node Information");
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(((Node) event.getSource()).getScene().getWindow());
stage.initStyle(StageStyle.UTILITY);
Parent root = FXMLLoader.load(url);
stage.setScene(new Scene(root));
stage.centerOnScreen();

textfield1.setValue("foo!");
textfield2.setValue("foo2");

stage.showAndWait();

Solution

  • Thank you Uluk Biy - your link led me to the answer which is:

    // get the controller from the loader
    SeedNodeFormController c = (SeedNodeFormController) fxmlLoader.getController();
    
    // call setter in controller routine to set needed values
    c.setSeedNode(value);