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);
}
}
@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