Skip to content

Instantly share code, notes, and snippets.

@pethaniakshay
Created June 8, 2017 19:43
Show Gist options
  • Save pethaniakshay/302072fda98098a24ce382a361bdf477 to your computer and use it in GitHub Desktop.
Save pethaniakshay/302072fda98098a24ce382a361bdf477 to your computer and use it in GitHub Desktop.
Switching between Scenes (Screens) in JavaFx using the FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="237.0" prefWidth="448.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="FXMLDocumentController">
<children>
<Button fx:id="btn2" layoutX="159.0" layoutY="126.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Click to go to scene 1" />
<Label layoutX="179.0" layoutY="71.0" text="You re in scene 2" />
</children>
</AnchorPane>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" style="-fx-background-color: red;" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="FXMLDocumentController">
<children>
<Button fx:id="btn1" layoutX="81.0" layoutY="121.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Click Here To goto scene 2" />
<Label layoutX="122.0" layoutY="55.0" prefHeight="14.0" prefWidth="77.0" text="This is Scene 1" />
</children>
</AnchorPane>
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class FXMLDocumentController implements Initializable {
@FXML
private Label lbl1,lbl2;
@FXML
private Button btn1,btn2;
@FXML
private void handleButtonAction (ActionEvent event) throws Exception {
Stage stage;
Parent root;
if(event.getSource()==btn1){
stage = (Stage) btn1.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("FXML2.fxml"));
}
else{
stage = (Stage) btn2.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TwoScene extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
@flowsurplus
Copy link

I keep getting a javafx.fxml.LoadException for the FXML2.fxml file

It's working for me. If you have a problem, it's maybe because of your package name. If the name of your package is "org.plann011.br" your link in the 2 FXML files should be "fx:controller="org.plann011.br.FXMLDocumentController"

You are honestly a godsend, thank you

@Ben-0-mad
Copy link

Thanks!

Also,

I keep getting a javafx.fxml.LoadException for the FXML2.fxml file

Make sure your URL to the FXML file is correct and that the FXML file has correct format.

@xkkpafi
Copy link

xkkpafi commented Nov 14, 2021

All I get is:m Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: inputStream is null.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2455)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
at replacepane.Main.loadMainPane(Main.java:50)
at replacepane.Main.start(Main.java:32)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Exception running application replacepane.Main

@NVG-064
Copy link

NVG-064 commented Jan 11, 2022

Thanks. This code works for switching scenes. But, my current status (example, my currency is $900) in previous scene will be wiped out. So, my currency will be $0 again in every switching scenes. However, thanks for sharing this code and helped me.

@muhammadfarras
Copy link

Thanks pal

@acerkuv
Copy link

acerkuv commented Aug 3, 2022

Many thanks for the solution. I passed mush time to reach the same the goal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment