title: JavaFX different behaviour between using FXML and straight Java
body: <p>I am fairly new to JavaFX and I am learning how to manipulate windows whilst resizing.  My goal is to have a window containing 4 sub windows that resize whilst the main window is resized.</p>
  
  <p>Enclosed are 2 minimal, complete,verifiable tests.  The first mcv1 uses Java to create the window, the second mcv2 uses fxml that was created using the Scene Builder. These were developed using Eclipse and need to be run from there.</p>
  
  <p>When using mcv1 the windows size correctly (see outputmcv1.jpg) whilst mcv2 the windows stop sizing (see outputmcv2.jpg).  The sub windows stop expanding.</p>
  
  <p>The real question is: Is there something in FXML that prevents Nodes expanding?  I wish to use FXML so I can have multilanguage support but this experiment could move me to native Java code.</p>
  
  <p>outputmcv1.jpg</p>
  
  <p><img src="https://i.stack.imgur.com/cDSm1.jpg" alt="outputmcv1.jpg[1]"></p>
  
  <p>outputmcv2.jpg
  <a href="https://i.stack.imgur.com/ODlDE.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ODlDE.jpg" alt="outputmcv2.jpg"></a></p>
  
  <p>Code files: Mcv1</p>
  
  <p>Mcv1.java</p>
  
  <pre><code>import javafx.application.Application;
  import javafx.beans.value.ChangeListener;
  import javafx.beans.value.ObservableValue;
  import javafx.scene.Group;
  import javafx.scene.Scene;
  import javafx.scene.control.Menu;
  import javafx.scene.control.MenuBar;
  import javafx.scene.layout.AnchorPane;
  import javafx.scene.layout.GridPane;
  import javafx.stage.Stage;
  
  public class Mcv1 extends  Application {
      public Mcv1() {
  
      }
      @Override     
         public void start(Stage stage) throws Exception { 
              Group root = new Group(); 
              Scene scene = new Scene(root);
              scene.getStylesheets().add(getClass().getResource("borders.css").toExternalForm());
              GridPane grid = new GridPane();
              AnchorPane menu = new AnchorPane();
              MenuBar bar = new MenuBar();
              Menu fileMenu = new Menu("File");
              bar.getMenus().add(fileMenu);
              menu.getChildren().add(bar);
              AnchorPane leftTop = new AnchorPane();
              leftTop.getStyleClass().add("red-border");
              AnchorPane leftBottom = new AnchorPane();
              leftBottom.getStyleClass().add("yellow-border");
              AnchorPane rightTop = new AnchorPane();
              rightTop.getStyleClass().add("blue-border");
              AnchorPane rightBottom = new AnchorPane();
              rightBottom.getStyleClass().add("green-border");
              root.getChildren().add(grid);
              grid.add(menu, 0, 0);
              grid.add(leftTop,0,1);
              grid.add(leftBottom,0,2);
              grid.add(rightTop,1,1);
              grid.add(rightBottom,1,2);
  
            // Creating a scene object 
            scene.widthProperty().addListener(new ChangeListener&lt;Number&gt;() {
                @Override
                public void changed(ObservableValue&lt;? extends Number&gt; observableValue, Number oldSceneWidth,Number newSceneWidth) {
                      Double widthD = newSceneWidth.doubleValue();
                      leftTop.setPrefWidth(widthD*0.4);
                      rightTop.setPrefWidth(widthD*0.6);
                      leftBottom.setPrefWidth(widthD*0.4);
                      rightBottom.setPrefWidth(widthD*0.6);
                      leftTop.setMaxWidth(widthD*0.4);
                      rightTop.setMaxWidth(widthD*0.6);
                      leftBottom.setMaxWidth(widthD*0.4);
                      rightBottom.setMaxWidth(widthD*0.6);
                }
            });
            scene.heightProperty().addListener(new ChangeListener&lt;Number&gt;() {
                @Override
                public void changed(ObservableValue&lt;? extends Number&gt; observableValue, Number oldSceneHeight,Number newSceneHeight) {
                      Double heightD = newSceneHeight.doubleValue();
                      leftTop.setPrefHeight(heightD*0.6);
                      rightTop.setPrefHeight(heightD*0.6);
                      leftBottom.setPrefHeight(heightD*0.4);
                      rightBottom.setPrefHeight(heightD*0.4);
                      leftTop.setMaxHeight(heightD*0.6);
                      rightTop.setMaxHeight(heightD*0.6);
                      leftBottom.setMaxHeight(heightD*0.4);
                      rightBottom.setMaxHeight(heightD*0.4); 
                }
  
            });
            // Setting title to the Stage   
            stage.setTitle("Jungle Finance"); 
  
            // Adding scene to the stage 
            stage.setScene(scene);
  
            //Displaying the contents of the stage 
            stage.show(); 
      }     public static void main(String args[]){           
            launch();      
         } 
  
  }
  </code></pre>
  
  <p>boders.css</p>
  
  <pre><code>red-border {
      -fx-background-color:red;
  }
  .green-border {
      -fx-background-color:green;
  }
  .yellow-border {
      -fx-background-color:yellow;
  }
  .blue-border {
      -fx-background-color:blue;
  }
  </code></pre>
  
  <p>Mcv2 code
  Mcv2.java</p>
  
  <pre><code>package com.mcv2;
  
  
  
  import javafx.application.Application;
  import javafx.beans.value.ChangeListener;
  import javafx.beans.value.ObservableValue;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  
  public class Mcv2 extends  Application {
  
      public Mcv2() {
  
      }
      @Override     
         public void start(Stage stage) throws Exception { 
  
              FXMLLoader loader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));
              Parent root = loader.load();
              ScreenController controller = loader.getController();
              Scene scene = new Scene(root);
  
  
            // Creating a scene object 
            scene.widthProperty().addListener(new ChangeListener&lt;Number&gt;() {
                @Override
                public void changed(ObservableValue&lt;? extends Number&gt; observableValue, Number oldSceneWidth,Number newSceneWidth) {
                      controller.setWidth(newSceneWidth);
                }
            });
            scene.heightProperty().addListener(new ChangeListener&lt;Number&gt;() {
                @Override
                public void changed(ObservableValue&lt;? extends Number&gt; observableValue, Number oldSceneHeight,Number newSceneHeight) {
                    controller.setHeight(newSceneHeight);
                }
  
            });
            // Setting title to the Stage   
            stage.setTitle("Jungle Finance"); 
  
            // Adding scene to the stage 
            stage.setScene(scene);
  
            //Displaying the contents of the stage 
            stage.show(); 
      }
        public static void main(String args[]){           
            launch();      
         } 
  
  }
  </code></pre>
  
  <p>ScreenController.java</p>
  
  <pre><code>package com.mcv2;
  
  
  import java.net.URL;
  import java.util.ResourceBundle;
  
  
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.control.MenuBar;
  import javafx.scene.layout.AnchorPane;
  
  public class ScreenController implements Initializable {
      @FXML
      private MenuBar menuBar;
      @FXML
      private AnchorPane leftTop;
      @FXML
      private AnchorPane leftBottom;
      @FXML
      private AnchorPane rightTop;
      @FXML
      private AnchorPane rightBottom;
      public ScreenController() {
  
      }
      @Override
      public void initialize(URL arg0, ResourceBundle arg1) {
  
      }
      @FXML
      private void window2Resized() {
      }
      @FXML
      private void fileOpenMenu () {
  
      }
      public void setWidth(Number width) {
          Double widthD = width.doubleValue();
          leftTop.setPrefWidth(widthD*0.4);
          rightTop.setPrefWidth(widthD*0.6);
          leftBottom.setPrefWidth(widthD*0.4);
          rightBottom.setPrefWidth(widthD*0.6);
          leftTop.setMaxWidth(widthD*0.4);
          rightTop.setMaxWidth(widthD*0.6);
          leftBottom.setMaxWidth(widthD*0.4);
          rightBottom.setMaxWidth(widthD*0.6);
          menuBar.setPrefWidth(widthD);
      }
      public void setHeight(Number height) {
          Double heightD = height.doubleValue();
          leftTop.setPrefHeight(heightD*0.6);
          rightTop.setPrefHeight(heightD*0.6);
          leftBottom.setPrefHeight(heightD*0.4);
          rightBottom.setPrefHeight(heightD*0.4);
          leftTop.setMaxHeight(heightD*0.6);
          rightTop.setMaxHeight(heightD*0.6);
          leftBottom.setMaxHeight(heightD*0.4);
          rightBottom.setMaxHeight(heightD*0.4);
      }
  }
  </code></pre>
  
  <p>MainScreen.fxml</p>
  
  <pre><code>?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import java.lang.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  
  &lt;GridPane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onDragDetected="#window2Resized" onDragDone="#window2Resized" onDragDropped="#window2Resized" onDragEntered="#window2Resized" onDragExited="#window2Resized" onDragOver="#window2Resized" onMouseDragEntered="#window2Resized" onMouseDragExited="#window2Resized" onMouseDragOver="#window2Resized" onMouseDragReleased="#window2Resized" onTouchMoved="#window2Resized" onTouchPressed="#window2Resized" onTouchReleased="#window2Resized" onTouchStationary="#window2Resized" prefHeight="507.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mcv2.ScreenController"&gt;
    &lt;columnConstraints&gt;
      &lt;ColumnConstraints hgrow="SOMETIMES" maxWidth="294.0" minWidth="10.0" prefWidth="140.0" /&gt;
      &lt;ColumnConstraints hgrow="SOMETIMES" maxWidth="460.0" minWidth="10.0" prefWidth="460.0" /&gt;
    &lt;/columnConstraints&gt;
    &lt;rowConstraints&gt;
        &lt;RowConstraints maxHeight="249.0" minHeight="10.0" prefHeight="54.0" vgrow="SOMETIMES" /&gt;
      &lt;RowConstraints maxHeight="403.0" minHeight="10.0" prefHeight="375.0" vgrow="SOMETIMES" /&gt;
      &lt;RowConstraints maxHeight="479.0" minHeight="10.0" prefHeight="105.0" vgrow="SOMETIMES" /&gt;
    &lt;/rowConstraints&gt;
     &lt;children&gt;
        &lt;AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnSpan="2"&gt;
           &lt;children&gt;
              &lt;MenuBar fx:id="menuBar" layoutY="2.0" prefHeight="25.0" prefWidth="346.0"&gt;
                &lt;menus&gt;
                  &lt;Menu mnemonicParsing="false" text="File"&gt;
                    &lt;items&gt;
                          &lt;MenuItem fx:id="fileOpen" mnemonicParsing="false" onAction="#fileOpenMenu" onMenuValidation="#fileOpenMenu" text="Open" /&gt;
                          &lt;MenuItem mnemonicParsing="false" text="Save" /&gt;
                          &lt;MenuItem mnemonicParsing="false" text="Save As" /&gt;
                      &lt;MenuItem mnemonicParsing="false" text="Close" /&gt;
                    &lt;/items&gt;
                  &lt;/Menu&gt;
                  &lt;Menu mnemonicParsing="false" text="Edit"&gt;
                    &lt;items&gt;
                      &lt;MenuItem mnemonicParsing="false" text="Delete" /&gt;
                    &lt;/items&gt;
                  &lt;/Menu&gt;
                    &lt;Menu mnemonicParsing="false" text="Window"&gt;
                      &lt;items&gt;
                        &lt;MenuItem mnemonicParsing="false" text="New" /&gt;
                          &lt;MenuItem mnemonicParsing="false" text="Close" /&gt;
                      &lt;/items&gt;
                    &lt;/Menu&gt;
                  &lt;Menu mnemonicParsing="false" text="Help"&gt;
                    &lt;items&gt;
                          &lt;MenuItem mnemonicParsing="false" text="Index" /&gt;
                      &lt;MenuItem mnemonicParsing="false" text="About" /&gt;
                    &lt;/items&gt;
                  &lt;/Menu&gt;
                &lt;/menus&gt;
              &lt;/MenuBar&gt;
           &lt;/children&gt;
        &lt;/AnchorPane&gt;
        &lt;AnchorPane fx:id="leftTop" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: red;" GridPane.rowIndex="1" /&gt;
        &lt;AnchorPane fx:id="rightTop" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: blue;" GridPane.columnIndex="1" GridPane.rowIndex="1" /&gt;
        &lt;AnchorPane fx:id="leftBottom" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: yellow;" GridPane.rowIndex="2" /&gt;
        &lt;AnchorPane fx:id="rightBottom" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: green;" GridPane.columnIndex="1" GridPane.rowIndex="2" /&gt;
     &lt;/children&gt;
  &lt;/GridPane&gt;
  </code></pre>
  
  <p>One last question.  I have all of the code with the Eclipse settings in a zip file but can't work out how to post it.  Any thoughts?</p>