title: Unable to get Scene from MenuItem in JavaFX
body: <p>I am trying to change the scene in a javafx stage based on menuItem click. Here is my sample.fxml:</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.geometry.Insets?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.control.Label?&gt;
  &lt;?import javafx.scene.image.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.layout.GridPane?&gt;
  
  &lt;AnchorPane prefHeight="-1.0" prefWidth="560.0" styleClass="background" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="sample.Controller"&gt;
    &lt;children&gt;
      &lt;MenuBar layoutY="0.0" maxWidth="1.7976931348623157E308" prefWidth="300.0" useSystemMenuBar="false" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="2.0"&gt;
        &lt;menus&gt;
          &lt;Menu id="manageAccountsMenu" mnemonicParsing="false" onAction="#onManageAccountsMenuActionPerformed" text="Accounts" fx:id="manageAccountsMenu"&gt;
            &lt;items&gt;
              &lt;MenuItem mnemonicParsing="false" onAction="#onTweetsMenuActionPerformed" text="Manage" fx:id="manageAccountsSubmenuItem" /&gt;
            &lt;/items&gt;
          &lt;/Menu&gt;
          &lt;Menu mnemonicParsing="false" onAction="#onTweetsMenuActionPerformed" text="Tweets" fx:id="tweetsMenuItem" /&gt;
          &lt;Menu mnemonicParsing="false" text="Retweets" /&gt;
        &lt;/menus&gt;
      &lt;/MenuBar&gt;
      &lt;VBox id="VBox" alignment="CENTER" layoutY="24.0" spacing="5.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"&gt;
        &lt;children&gt;
          &lt;ScrollPane id="ScrollPane" fitToHeight="true" fitToWidth="true" prefViewportHeight="400.0" prefViewportWidth="300.0"&gt;
            &lt;content&gt;
              &lt;TableView prefHeight="-1.0" prefWidth="-1.0" tableMenuButtonVisible="true"&gt;
                &lt;columns&gt;
                  &lt;TableColumn editable="false" prefWidth="75.0" text="SNO" /&gt;
                  &lt;TableColumn prefWidth="200.0" text="Account" /&gt;
                  &lt;TableColumn prefWidth="200.0" text="Status" /&gt;
                  &lt;TableColumn prefWidth="75.0" text="Actions" /&gt;
                &lt;/columns&gt;
              &lt;/TableView&gt;
            &lt;/content&gt;
          &lt;/ScrollPane&gt;
          &lt;Button mnemonicParsing="false" text="Add Account" textAlignment="CENTER"&gt;
            &lt;graphic&gt;
              &lt;ImageView fitHeight="150.0" fitWidth="200.0" mouseTransparent="true" pickOnBounds="true" preserveRatio="true"&gt;
                &lt;image&gt;
                  &lt;Image url="@addAccount.png" /&gt;
                &lt;/image&gt;
              &lt;/ImageView&gt;
            &lt;/graphic&gt;
          &lt;/Button&gt;
        &lt;/children&gt;
      &lt;/VBox&gt;
    &lt;/children&gt;
    &lt;stylesheets&gt;
      &lt;URL value="@darkTheme.css" /&gt;
    &lt;/stylesheets&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>Here is my Controller.java:</p>
  
  <pre><code>package sample;
  
  import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.Node;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.MenuItem;
  import javafx.stage.Stage;
  
  import java.net.URL;
  import java.util.ResourceBundle;
  
  public class Controller implements Initializable {
      @FXML
      protected void onManageAccountsMenuActionPerformed(ActionEvent event) {
          System.out.println("Manage Accbtnclick");
  //        Node node=(Node) event.getSource();
  //        Stage stage=(Stage) node.getScene().getWindow();
  //
  //        Scene scene = new Scene(root);
  //        stage.setScene(scene);
  //        stage.show();
      }
      @FXML
      protected void onTweetsMenuActionPerformed(ActionEvent event) {
          System.out.println("Manage Accbtnclick");
      Node node= (Node)event.getSource();
      Stage stage=(Stage) node.getScene().getWindow();
      Scene scene = Main.screens.get("tweet");
      stage.setScene(scene);
      stage.show();
      }
  
      @Override
      public void initialize(URL url, ResourceBundle resourceBundle) {
          //To change body of implemented methods use File | Settings | File Templates.
      }
  }
  </code></pre>
  
  <p>Here is my Main.java:</p>
  
  <pre><code>package sample;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.geometry.Pos;
  import javafx.scene.Group;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.Label;
  import javafx.scene.control.ProgressBar;
  import javafx.scene.control.ProgressIndicator;
  import javafx.scene.layout.HBox;
  import javafx.scene.layout.VBox;
  import javafx.stage.Stage;
  
  import java.io.IOException;
  import java.util.HashMap;
  
  public class Main extends Application {
  
      public static HashMap&lt;String,Scene&gt; screens=new HashMap&lt;String,Scene&gt;();
  
      @Override
      public void start(Stage stage) {
          try {
              Parent accountScreen= FXMLLoader.load(getClass().getResource("sample.fxml"));
              Parent tweetScreen=FXMLLoader.load(getClass().getResource("tweetform.fxml"));
              //Parent retweetScreen=FXMLLoader.load(getClass().getResource("retweetform.fxml"));
              screens.put("account",new Scene(accountScreen));
              screens.put("tweet",new Scene(tweetScreen));
              //screens.put("retweet",new Scene(retweetScreen));
              stage.setTitle("Manage Accounts");
              stage.setScene(screens.get("account"));
              stage.show();
          } catch (IOException e) {
              e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
          }
  
      }
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>When I click on the menuItem Manage under Accounts Menu, I get the following exception:</p>
  
  <pre><code>    "C:\Program Files\Java\jdk1.7.0_17\bin\java" -Didea.launcher.port=7541 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_17\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext\zipfs.jar;C:\Users\rahulserver\IdeaProjects\DrawingText\out\production\DrawingText;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.1.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain sample.Main
  Manage Accbtnclick
  Manage Accbtnclick
  java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
      at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1440)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
      at javafx.event.Event.fireEvent(Event.java:171)
      at javafx.scene.control.MenuItem.fire(MenuItem.java:456)
      at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1188)
      at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$6.handle(ContextMenuContent.java:1139)
      at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$6.handle(ContextMenuContent.java:1137)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
      at javafx.event.Event.fireEvent(Event.java:171)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3328)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
      at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
      at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
      at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
      at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
      at com.sun.glass.ui.View.notifyMouse(View.java:922)
      at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
      at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
      at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:55)
      at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:269)
      at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1435)
      ... 40 more
  Caused by: java.lang.ClassCastException: javafx.scene.control.MenuItem cannot be cast to javafx.scene.Node
      at sample.Controller.onTweetsMenuActionPerformed(Controller.java:29)
      ... 50 more
  </code></pre>
  
  <p>So how do I get the containing stage/scene from the menu Item clicked event handler?</p>
  
  <p><strong>EDIT</strong>
  The line         </p>
  
  <pre><code>Node node= (Node)event.getSource();
  </code></pre>
  
  <p>in controller.java is line number 29 which is giving problems. </p>