0

How can I switch between different scenes loaded from different FXML files? I got a working example, but when it switches between the scenes, the scene is in non-fullscreen mode for a moment and then goes back to fullscreen. If my solution is not a good way to do it, can you provide some alternatives? How do apps developed in JavaFX switch between the main scene and a menu scene for example? There is a similar post to mine but nobody provided a solution since 2016.

Here is my code:

SwitchScenesExample.java

public class SwitchScenesExample extends Application {
  static double resWidth = Screen.getPrimary().getBounds().getWidth();
  static double resHeight = Screen.getPrimary().getBounds().getHeight();

  @Override
  public void start(Stage stage) throws IOException {
    FXMLLoader fxmlLoader = new FXMLLoader(SwitchScenesExample.class.getResource("scene2.fxml"));
    Scene scene = new Scene(fxmlLoader.load(), resWidth, resHeight);
    Controller controller = fxmlLoader.getController();
    controller.setNextScene("scene1.fxml");
    stage.setTitle("SwitchingFXML");
    stage.setScene(scene);
    stage.setFullScreen(true);
    stage.show();
  }

  public static void main(String[] args) {
    launch();
  }
}

Controller.java

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.io.IOException;

public class Controller {
  private String nextScene;
  @FXML
  private Button sceneButton;
  @FXML
  private GridPane gridPane;

  public void setNextScene(String fileName) {
    this.nextScene = fileName;
  }
  @FXML
  void onSceneButtonClicked(ActionEvent event) {
    try {
      FXMLLoader loader = new FXMLLoader(getClass().getResource(nextScene));
      Stage stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
      Scene scene = new Scene(loader.load(), SwitchScenesExample.resWidth, SwitchScenesExample.resHeight);
      if(nextScene.equals("scene1.fxml")) {
        Controller controller = loader.getController();
        controller.setNextScene(nextScene = "scene2.fxml");
      } else {
        Controller controller = loader.getController();
        controller.setNextScene(nextScene = "scene1.fxml");
      }
      stage.setFullScreen(true);
      stage.setScene(scene);
    }catch (IOException io){
      io.printStackTrace();
    }
  }
}

scene1.fxml


<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<GridPane fx:id="gridPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.mrec.Controller">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
            <Button fx:id="sceneButton" alignment="CENTER" mnemonicParsing="false" onAction="#onSceneButtonClicked" text="Other Scene" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER" />
      <Label alignment="CENTER" text="Scene1" textFill="#ae1a1a" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <Label alignment="CENTER" text="Scene1" textFill="#1d6b9e" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
   </children>
</GridPane>

scene2.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<GridPane fx:id="gridPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.mrec.Controller">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>

            <Button fx:id="sceneButton" alignment="CENTER" mnemonicParsing="false" onAction="#onSceneButtonClicked" text="Other Scene" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER" />
      <Label alignment="CENTER" text="Scene2" textFill="#ae1a1a" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <Label alignment="CENTER" text="Scene2" textFill="#1d6b9e" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER" />
   </children>
</GridPane>
Ernest P W
  • 41
  • 5
  • 3
    Do you really need a new scene? Can't you just set the root of the existing scene? All you need is `gridPane.getScene().setRoot(loader.load())`. – James_D Jun 10 '22 at 18:20
  • 3
    https://stackoverflow.com/questions/49223773/changing-scenes-in-full-screen – James_D Jun 10 '22 at 18:21

0 Answers0