1

I have a class named Test1Controller where a Scene is executed and in another class, Test2Controller, I want to load another FXML but in the same Scene. Can someone help me with this?

public class Test1Controller {

    private static AnchorPane page;

    public static Stage initialicePage() {

        primaryStage = new Stage();
        page = (AnchorPane) FXMLLoader.load(Principal.class.getResource("Test1.fxml"));
        Scene scene = new Scene(page);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Test1");
        primaryStage.setResizable(true);
        primaryStage.setMinHeight(700);
        primaryStage.setMinWidth(824);
        primaryStage.setMaximized(true);
        primaryStage.show();
    }
}
Slaw
  • 37,820
  • 8
  • 53
  • 80
apr_dev
  • 73
  • 1
  • 9
  • Possible duplicate of [Loading new fxml in the same scene](https://stackoverflow.com/questions/18619394/loading-new-fxml-in-the-same-scene) – EHM Feb 20 '19 at 19:33
  • https://stackoverflow.com/questions/16176701/switch-between-panes-in-javafx?noredirect=1&lq=1 – Slaw Feb 20 '19 at 21:11

1 Answers1

1
public class SwitchScene 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);
    }
}

public class FXMLDocumentController {
    @FXML
    private Label label;
    
    @FXML
    private void handleButtonAction(ActionEvent event) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("SecondScreen.fxml"));
            Scene dashboard=new Scene(root);
            //This line gets the Stage Information
            Stage window=(Stage)((Node)event.getSource()).getScene().getWindow();
            window.setScene(dashboard);
            window.show();
        } catch (IOException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

public class SecondScreenController {
    public void MoveBack(ActionEvent event){
        try {
            Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
            
            Scene dashboard = new Scene(root);
            //This line gets the Stage Information
            //here we get the stage from event action and setting the root element in the scene and display scene with stage object (window) which is retrieved from action event
            Stage window=(Stage)((Node)event.getSource()).getScene().getWindow();
            window.setScene(dashboard);
            window.show();
        } catch (IOException ex) {
            Logger.getLogger(SecondScreenController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="switchscene.FXMLDocumentController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
    </children>
</AnchorPane>

    

////SecondScreen fxml

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="switchscene.SecondScreenController">
   <children>
      <Button fx:id="btnMove" layoutX="269.0" layoutY="188.0" mnemonicParsing="false" onAction="#MoveBack" text="Move Back" />
   </children>
</AnchorPane>
jewelsea
  • 150,031
  • 14
  • 366
  • 406
Haroon
  • 11
  • 3
  • Note that this answer actually creates a new scene, but to change to reuse an existing scene is trivial, in the button action handler initiating the scene transition, write `((Node) event.getSource()).getScene().setRoot(FXMLLoader.load(getClass().getResource("SecondScreen.fxml")))`. – jewelsea Feb 18 '22 at 00:34