title: Creating a JavaFX Scene from Button click; Close the created Scene
body: <p>I am learning JavaFX, and creating a scene that will be created and displayed to get user input. I created a VBox that holds different textfields and a button that I want to use to close the scene that was created and then store that information to use elsewhere. I can't seem to figure out how to add functionality to the button that will close the scene.</p>
  <p>I've tried adding a EventHandler that will do it, but I can't seem to get the program to work.</p>
  <p><strong>part of Controller</strong></p>
  <pre><code>public void drawNewClass(ActionEvent actionEvent) throws IOException {
          ClassInstance classInstance = new ClassInstance();
          TextField classDescription = new TextField(&quot;Enter      Description&quot;);
          TextField className = new TextField(&quot;Enter Class Name&quot;);
          Button close = new Button(&quot;Submit&quot;);
          close.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
              @Override
              public void handle(ActionEvent event) {
                  
              }
          });
          TextField attributes = new TextField(&quot;Enter Variables (privacy type name, format)&quot;);
          VBox secondaryLayout = new VBox();
          secondaryLayout.getChildren().addAll(classDescription, className, attributes, close);
          Scene secondScene = new Scene(secondaryLayout, 230, 100);
  
          // New window (Stage)
          Stage newWindow = new Stage();
          newWindow.setTitle(&quot;Add Class&quot;);
          newWindow.setScene(secondScene);
  
          // Set position of second window, related to primary window.
          newWindow.setX(canvas.getLayoutX() + 200);
          newWindow.setY(canvas.getLayoutY() + 100);
  
          newWindow.showAndWait();
  </code></pre>
  <p><strong>.FXML</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;?import javafx.scene.layout.VBox?&gt;
  &lt;?import javafx.scene.layout.BorderPane?&gt;
  &lt;?import javafx.scene.layout.HBox?&gt;
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.control.ScrollPane?&gt;
  &lt;?import javafx.scene.canvas.Canvas?&gt;
  &lt;?import javafx.scene.layout.StackPane?&gt;
  &lt;BorderPane xmlns=&quot;http://javafx.com/javafx/17&quot;
              xmlns:fx=&quot;http://javafx.com/fxml/1&quot;
              fx:controller=&quot;com.diagrambuilder.csc260w2022project2.diagramBuilderController&quot;
              prefHeight=&quot;500.0&quot; prefWidth=&quot;500.0&quot;
              fx:id=&quot;root&quot;&gt;
      &lt;top&gt;
          &lt;HBox&gt;
              &lt;Button text=&quot;Close&quot; fx:id=&quot;closeButton&quot; onAction=&quot;#closeApplication&quot;/&gt;
              &lt;Button text=&quot;New&quot; onAction=&quot;#newCanvas&quot;/&gt;
              &lt;Button text=&quot;Save&quot; onAction=&quot;#saveCanvas&quot;/&gt;
              &lt;Button text=&quot;Save As&quot; onAction=&quot;#saveAsCanvas&quot;/&gt;
              &lt;Button text=&quot;Load&quot; onAction=&quot;#loadCanvas&quot;/&gt;
              &lt;Button text=&quot;Export as Image&quot; onAction=&quot;#exportCanvas&quot;/&gt;
          &lt;/HBox&gt;
      &lt;/top&gt;
      &lt;left&gt;
          &lt;VBox&gt;
              &lt;Button text=&quot;Draw New Class&quot; fx:id=&quot;newClass&quot; onAction=&quot;#drawNewClass&quot;/&gt;
              &lt;Button text=&quot;Add New Relationship&quot; onAction=&quot;#drawNewRelationship&quot;/&gt;
          &lt;/VBox&gt;
      &lt;/left&gt;
      &lt;center&gt;
          &lt;StackPane fx:id=&quot;stackPane&quot; style=&quot;-fx-border-color: #000; -fx-border-width: 1px&quot;
                      maxWidth=&quot;500&quot; maxHeight=&quot;500&quot;&gt;
              &lt;Canvas fx:id=&quot;canvas&quot; width=&quot;${stackPane.width}&quot; height=&quot;${stackPane.height}&quot;/&gt;
          &lt;/StackPane&gt;
      &lt;/center&gt;
  &lt;/BorderPane&gt;
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/BVZIm.png" rel="nofollow noreferrer">Image of GUI</a></p>