title: JavaFX Panel inside Panel auto resizing
body: <p>I have a JavaFX application which has only one FXML file. In this file I have one AnchorPane which has a StackPane inside it. Here is the screenshot:</p>
  
  <p><img src="https://i.stack.imgur.com/xtysq.png" alt="my panel inside panel application"></p>
  
  <p>When I start this application, I want to resize StackPane automatically with the AnchorPane. Thus; StackPane will get the current avaliable width and height automatically. At the moment when I resize the application, AnchorPane is being resized automatically but StackPane stays as his fixed size.</p>
  
  <p>How can I resize the StackPane automatically and make it fully stretched inside its parent panel?</p>
  
  <p><strong>My Code</strong></p>
  
  <p><em>Main.java</em></p>
  
  <pre><code>package app;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  
  public class Main extends Application {
  
    public static void main(String[] args) {
        Application.launch(args);
    }
  
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
        Scene scene = new Scene(root,800,600);
        scene.getStylesheets().add(this.getClass().getResource("/app/style1.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
    }
  }
  </code></pre>
  
  <p><em>MainController.java</em></p>
  
  <pre><code>package app;
  
  import java.net.URL;
  import java.util.ResourceBundle;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.layout.AnchorPane;
  import javafx.scene.layout.StackPane;
  
  public class MainController implements Initializable {
  
     @FXML
     private AnchorPane anchorPane;
     @FXML
     private StackPane stackPane;
  
     @Override
     public void initialize(URL url, ResourceBundle rb) {
       stackPane.setPrefSize(anchorPane.getPrefWidth(), anchorPane.getPrefHeight()); //didn't work
     }    
  }
  </code></pre>
  
  <p><em>Main.fxml</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;?import java.lang.*?&gt;
  &lt;?import java.util.*?&gt;
  &lt;?import javafx.scene.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  
  &lt;AnchorPane fx:id="anchorPane" xmlns:fx="http://javafx.com/fxml" fx:controller="app.MainController"&gt;
      &lt;StackPane fx:id="stackPane" &gt;&lt;/StackPane&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p><em>style1.css</em></p>
  
  <pre><code>#anchorPane { 
      -fx-border-width: 2px;
      -fx-border-color: chartreuse;
  }
  #stackPane {
      -fx-border-width: 2px;
      -fx-border-color: red;
  
      /* didn't work */
     -fx-hgap: 100%;
     -fx-vgap: 100%;
  }
  </code></pre>