I have looked on many pages to try and find out how to switch scenes but I have been unsuccessful.
I have a calculator and my goal is to select a menu option to change Calculators(ie: basic and scientific). Right now I am just testing so here is my code relevant to this question thus far (I am using Scene Builder):
@FXML private MenuItem basic;
@FXML private MenuItem testSwitch;
public static void main(String[] args)
{
Application.launch( args );
}
@Override
public void start(Stage primaryStage) throws Exception
{
Parent pane = FXMLLoader.load(
getClass().getResource( "calculator.fxml" ) );
Scene scene = new Scene( pane );
primaryStage.setScene(scene);
primaryStage.setTitle( "Calculator" );
primaryStage.show();
}
@FXML
public void handleMenuOption(ActionEvent e)
{
if(e.getSource()==basic)
{
changeScene("calculator.fxml");
}
else if(e.getSource()==testSwitch)
{
changeScene("TestSwitch.fxml");
}
}
public void changeScene(String fxml)
{
//this prints out
System.out.println(fxml);
}
EDIT I've tried quite a few things already. No matter what, I always get this NullPointerException. I have a feeling it may have to do with setting something in scene builder but I just have not been able to find an answer
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.control.MenuItem.fire(Unknown Source)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(Unknown Source)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
... 44 more
Caused by: java.lang.NullPointerException
at CalculatorMain.changeScene(CalculatorMain.java:75)
at CalculatorMain.handleMenuOption(CalculatorMain.java:64)
... 53 more
at CalculatorMain.changeScene(CalculatorMain.java:75)
This is at:stage . getScene() . setRoot(pane);
at CalculatorMain.handleMenuOption(CalculatorMain.java:64)
This is at:changeScene ("TestSwitch.fxml");
WORKING CODE:
I played around using suggestions below and used this code to make it work:
private Stage stage;
public static void main(String[] args)
{
Application.launch( args );
}
@Override
public void start(Stage primaryStage) throws Exception
{
this.stage = primaryStage;
FXMLLoader loader = new FXMLLoader(getClass()
.getResource("calculator.fxml"));
Parent root = (Parent)loader.load();
BasicCalculatorView controller = (BasicCalculatorView)loader.getController();
controller.setModel(new BasicCalculatorModelTest(controller));
controller.setLogic(this);
primaryStage.setTitle("Calculator");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public void switchScene(String fxmlFile)
{
FXMLLoader loader = new FXMLLoader(getClass()
.getResource(fxmlFile));
Parent root;
try
{
root = (Parent)loader.load();
if(fxmlFile.equals("calculator.fxml"))
{
BasicCalculatorView controller = (BasicCalculatorView)loader.getController();
controller.setModel(new BasicCalculatorModelTest(controller));
controller.setLogic(this);
}
else if(fxmlFile.equals("TestSwitch.fxml"))
{
TestSwitch controller = (TestSwitch)loader.getController();
controller.setLogic(this);
}
this.stage.setScene(new Scene(root));
}
catch (IOException e)
{
e.printStackTrace();
}
}
If you want to go along with changing the scene you would do it like this (note that the Stage is a member of the application):
However as already pointed out by @Eugene_Ryzhikov it is a better solution to just change the root content of the existing scene:
It seems OP already solved it but since it is still open and unsolved i will share a solution i found in another answer. Remember to either select an correct answer or post your own solution when a question is solved, it helps people in the future that runs in the same issue (like i did).
I just run in the same issue and this answer solved my issue perfectly while being short and clean.
PS.: Remember to check the original answer and upvote it. The guy deserves...
PPS.: I am not sure just copying an answer is okay (instead of just sharing the link through a comment) but since this doesnt have a correct answer yet i decided to do it for visibility.
Instead of switching
Scenes
, switch a root node on already existingScene
I wrote this controller to keep track of the different scenegraphes.
So I can write:
This was a workaround for a fullscreen application, where the MacOS fullscreen transition was shown every time a stage switches its scene.