title: JavaFX: Listen to Scene changes
body: <p>How can I make a custom Event that triggers on <code>Stage.setScene()</code>?<br>
  In my code, the button switches the Scenes and that works fine. However, I would like to extend the Stage to have an additional Event that is triggered when a button or possibly any other Element triggers a setScene.</p>
  
  <p>Example:</p>
  
  <pre class="lang-java prettyprint-override"><code>package sample;
  
  import javafx.application.Application;
  import javafx.scene.Group;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.stage.Stage;
  
  public class Main extends Application {
  
      public static void main(String[] args) {
          Application.launch(args);
      }
  
      @Override
      public void start(Stage stage) {
          Group g1 = new Group();
          Button b1 = new Button("2");
          g1.getChildren().setAll(b1);
          Scene scene1 = new Scene(g1, 50, 50);
  
          Group g2 = new Group();
          Button b2 = new Button("1");
          g2.getChildren().setAll(b2);
          Scene scene2 = new Scene(g2, 50, 50);
  
          stage.setScene(scene1);
          stage.setTitle("JavaFX Application Life Cycle");
  
          b1.setOnAction(actionEvent -&gt; {
              System.out.println("1");
              stage.setScene(scene2);
          });
          b2.setOnAction(actionEvent -&gt; {
              System.out.println("2");
              stage.setScene(scene1);
          });
          stage.show();
      }
  }
  </code></pre>