javajavafxscenestage

When switching the javaFX scene, size of stage is getting Minimize


I have added a scene to primaryStage in a javaFx project, in its Main method. Also in the same main class, the Stage is primaryStage.setMaximized(true); Prepared as then by clicking a button in the fxml code related to that scene, the existing scene on the stage will be changed.

The problem is that the stage is automatic minimized when the scene changes. I hope to keep the default size of the stage maximized.

Here is the starting main class of my project

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {

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


    @Override
    public void start(Stage primaryStage) throws IOException {
        Parent parent = FXMLLoader.load(getClass().getResource("MainScene.fxml"));
        Scene scene = new Scene(parent);
        primaryStage.setScene(scene);
        primaryStage.setMaximized(true);
        primaryStage.show();
    }
}

Here is the MainScene.fxml code that runs through the main class

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MainSceneConn">
   <children>
      <Text layoutX="289.0" layoutY="196.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Form" />
      <Button fx:id="button" layoutX="236.0" layoutY="213.0" mnemonicParsing="false" onAction="#loadNewScene" prefHeight="25.0" prefWidth="134.0" text="New Scene" />
   </children>
</AnchorPane>

Here is the controller class for that MainScene.fxml file.

import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class MainSceneConn {
    public Button button;

    public void loadNewScene(ActionEvent actionEvent) throws IOException {

        Stage stage = (Stage) button.getScene().getWindow();

        stage.setMaximized(true);//this line is not work :(

        Parent parent = FXMLLoader.load(getClass().getResource("secondScene.fxml"));
        Scene scene = new Scene(parent);
        stage.setScene(scene);
    }
}

Here is the fxml file that contains the scene through that controller class

<?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 xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            prefHeight="400.0" prefWidth="600.0">

</AnchorPane>

Outside of my main method, again inside the MainScene.fxml controller class stage.setMaximized(true); prepared as But it is no different.


Solution

  • I do not know why the maximize state of the stage changes when you set a new scene on the stage.

    Instead of creating a new scene, you can change the root of the existing scene, then the maximize state of the window will remain the same.

    import javafx.event.ActionEvent;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    
    import java.io.IOException;
    
    public class MainSceneConn {
        public Button button;
    
        public void loadNewScene(ActionEvent actionEvent) throws IOException {
            Parent parent = FXMLLoader.load(getClass().getResource("secondScene.fxml"));
    
            Scene scene = button.getScene();
            scene.setRoot(parent);
        }
    }
    

    The setScene documentation states:

    If the width or height of this Window have never been set by the application, setting the scene will cause this Window to take its width or height from that scene. Resizing this window by end user does not count as setting the width or height by the application.

    I am just guessing that maximizing the window in code also does not count as setting the width or height of the window, and also in that case setting the scene will "cause this Window to take its width or height from that scene", essentially reverting the maximized setting. It is kind of strange behavior though.

    Consider whether, instead of setting the window to maximized, it would not be better to set it to full screen. For some applications that might be better, but for other applications, it may not be better.