How to run default code(initialize) when a new window opens in JavaFX
<p>I Want a way to run some code when i open a NewWindow from the main Controller in JavaFX . </p>

<p>Here is my main controller have :</p>

<pre><code>public class FXMLDocumentController implements Initializable {
//consider all variables and or elements initialized
final Stage newWindow = new Stage();

@FXML
private void searchButtonAction(ActionEvent event)  {
    //Clicking on Search button triggers this event handler and Search Window will open with all the results
    newWindow.show();
}

@Override
public void initialize(URL url, ResourceBundle rb) {

    try {
        newWindow.initModality(Modality.APPLICATION_MODAL);
        //Loader to load the Search Window
        FXMLLoader loader = new FXMLLoader(
                getClass().getResource("SearchWindow.fxml")
        );
        Scene scene = new Scene(loader.load());
        newWindow.setScene(scene);
        SearchWindowController controller = loader.&lt;SearchWindowController&gt;getController();
        controller.mainWindow = this;

    } catch (Exception e) {
        System.err.println(e.getMessage());
    }`
}`}`
</code></pre>

<p>Here is my new window controller :</p>

<pre><code>public class SearchWindowController implements Initializable {

@FXML
void searchButtonAction() {
    //Code i want to run when this window opens
    } catch (Exception e) {
        System.err.print(e);
    }
}

/**
 * Initializes the controller class.
 *
 * @param url
 * @param rb
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    //empty 
    //call searchButtonAction() when this window opens ??? 
}
</code></pre>

<p>So basically the new window opens but i want to run some code i have when it loads . I have gone through some places and have noted that i can make a start method which should run when the new window starts. But i am unable to get it to run in my code . Also an other method i have seen is putting an event handler . </p>

<p>My question again being if I were to implement the above methods, where exactly should i put those method/Event Handler.I want my searchButtonAction() to be called by Default when the window loads. A different approach would also help. Some examples would be appreciated.</p>

<p>Edit:I want the output to be displayed on the newWindow , its basically some text that is taken from the main window and displays it in the new window . (Currently how i am working around it is creating a new button in the new window and then putting a onClickAction event on the button to run searchButtonAction() ). I want searchButtonAction() to run by Default . How do I initialize it so that searchButtonAction() is called when I open that new window.</p>

<p>Thank you in advance.</p>