title: JavaFX 2.0 FXML Child Windows
body: <p>After much searching I found this question <a href="https://stackoverflow.com/questions/10915388/how-to-create-a-javafx-2-0-application-mdi">How to create a javafx 2.0 application MDI</a>.  What I really wanted to know is if I can create a pop-up window or child window to the main window using JavaFX components and Scene Builder to create the new window.</p>
  
  <p>I ended up with this for a modal pop-up window:</p>
  
  <p>In the Main class I wanted to save the primary stage to a field I can access from my primary controller class.  So, I added a static variable <strong>Stage</strong> to it and this in the <strong>Main.Start()</strong> method:</p>
  
  <pre><code>primaryController.primaryStage = primaryStage;
  </code></pre>
  
  <p>This the method that a button in the primaryController uses:</p>
  
  <pre><code>public void OnBtnShowChild(ActionEvent event) {
      MessageBoxController msgBox = new MessageBoxController();
      try {
          msgBox.showMessageBox(primaryStage);
      } catch (Exception e) {
          e.printStackTrace(); 
      }
  }
  </code></pre>
  
  <p>This is the MessageBoxController class that I created with help from Scene Builder.  It has the basic layout of a standard pop-up box that can be used to display an Icon (ImageView), TextBox (for your message text), and two buttons (for YES/NO functionality).  I am not sure yet how to have it communicate the results of what button was pressed back to the primaryController.</p>
  
  <pre><code>public class MessageBoxController implements Initializable {
  
  @FXML
  // fx:id="btnNo"
  private Button btnNo; // Value injected by FXMLLoader
  
  @FXML
  // fx:id="btnYes"
  private Button btnYes; // Value injected by FXMLLoader
  
  @FXML
  // fx:id="imgMessage"
  private ImageView imgMessage; // Value injected by FXMLLoader
  
  @FXML
  // fx:id="txtMessage"
  private TextField txtMessage; // Value injected by FXMLLoader
  
  private Stage myParent;
  private Stage messageBoxStage;
  
  public void showMessageBox(Stage parentStage) {
      this.myParent = parentStage;
  
      try {
          messageBoxStage = new Stage();
          AnchorPane page = (AnchorPane) FXMLLoader.load(MessageBoxController.class.getResource("/MessageBox/MessageBoxFXML.fxml"));
          Scene scene = new Scene(page);
          messageBoxStage.setScene(scene);
          messageBoxStage.setTitle("Message Box");
          messageBoxStage.initOwner(this.myParent);
          messageBoxStage.initModality(Modality.WINDOW_MODAL);
          messageBoxStage.show();
      } catch (Exception ex) {
          System.out.println("Exception foundeth in showMessageBox");
          ex.printStackTrace();
      }
  }
  @Override
  public void initialize(URL fxmlFileLocation, ResourceBundle arg1) {
      txtMessage.setText("Howdy");
  
  }
  
  public void OnBtnYes(ActionEvent event) {
  
  }
  
  public void OnBtnNo(ActionEvent event) {
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And finally, this is the FXML file I created in Scene Builder:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import java.lang.*?&gt;
  &lt;?import java.net.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.image.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  
  &lt;AnchorPane id="AnchorPane2" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"   
        prefHeight="172.0" prefWidth="524.0" xmlns:fx="http://javafx.com/fxml" fx:controller="MessageBox.MessageBoxController"&gt;
    &lt;children&gt;
      &lt;VBox prefHeight="172.0" prefWidth="524.0" styleClass="vboxes" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt;
        &lt;children&gt;
          &lt;HBox alignment="CENTER" prefHeight="109.99990000000253" prefWidth="516.0" spacing="30.0"&gt;
            &lt;children&gt;
              &lt;ImageView fx:id="imgMessage" fitHeight="110.0" fitWidth="146.66666666666666" pickOnBounds="true" preserveRatio="true" styleClass="null" /&gt;
              &lt;TextField fx:id="txtMessage" editable="false" prefHeight="47.0" prefWidth="325.0" /&gt;
            &lt;/children&gt;
            &lt;stylesheets&gt;
          &lt;URL value="@MyCSS.css" /&gt;
        &lt;/stylesheets&gt;
      &lt;/HBox&gt;
      &lt;HBox alignment="CENTER" prefHeight="58.0" prefWidth="516.0" spacing="30.0"&gt;
        &lt;children&gt;
          &lt;Button fx:id="btnYes" mnemonicParsing="false" onAction="#OnBtnYes" text="Button" /&gt;
          &lt;Button fx:id="btnNo" mnemonicParsing="false" onAction="#OnBtnNo" text="Button" /&gt;
        &lt;/children&gt;
      &lt;/HBox&gt;
    &lt;/children&gt;
    &lt;stylesheets&gt;
      &lt;URL value="@MyCSS.css" /&gt;
    &lt;/stylesheets&gt;
  &lt;/VBox&gt;
  &lt;/children&gt;
  &lt;stylesheets&gt;
  &lt;URL value="@MyCSS.css" /&gt;
  &lt;/stylesheets&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>With this I can create a modal pop-up window, and I also want to create other child windows for displaying data in other ways using different controls.  And, most importantly, I can use Scene Builder to create the layout.</p>
  
  <p>What do you think?  Is this a good way to do this until they add real support in Java 8 and JavaFX 8?</p>