Home:ALL Converter>scene not fitting in the stage

scene not fitting in the stage

Ask Time:2013-02-18T22:46:55         Author:user1090694

Json Formatter

I load the stage the following way:

public void start(Stage stage) throws Exception{
    stage.setTitle("title");
    Scene scene = (Scene)FXMLLoader.load(getClass().getResource("../view/MainMenu.fxml"));
    stage.setScene(scene);
    stage.setWidth(1080);
    stage.setHeight(720);
    stage.setFullScreen(false);
    stage.show();
}

Change Scene:

@FXML protected void click(ActionEvent event) throws Exception{
    Stage stage = (Stage)menu.getScene().getWindow();
    Scene scene = (Scene)FXMLLoader.load(getClass().getResource("../view/MainMenu.fxml"));
    stage.setScene(scene);
    stage.show();
}

Fxml:

<Scene fx:controller="controller.CtrlMainMenu" xmlns:fx="http://javafx.com/fxml" stylesheets="view/Style.css">
    <AnchorPane fx:id="menu">
        <VBox spacing="8"
              AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0"
              AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
            <Button text="click" onAction="#click"/>
        </VBox>
    </AnchorPane>
</Scene>

The problem comes when changing the scene; the new scene is smaller and appears on top left and not fully fitting the window (window keeps its size, and after resizing the window, the scene refits again to the window, but before, not).

Also, is it possible to send an object to the controller of the view, since I'll need to work with the model with the controller? (using MVC architecture)


Now tried this method 2 with fx:root and have the following:

Aplication:

public void start(Stage stage) throws Exception{
    FXMLLoader fxmlLoader = new FXMLLoader();
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/MainMenu.fxml").openStream());
    CtrlMainMenu controller = (CtrlMainMenu) fxmlLoader.getController();
    controller.sendString("test");      //send object test
    stage.setScene(new Scene(p));

    stage.setWidth(1080);
    stage.setHeight(720);
    stage.setFullScreen(false);
    stage.show();
}

Fxml:

<fx:root type="javafx.scene.layout.AnchorPane" xmlns:fx="http://javafx.com/fxml" fx:controller="controller.CtrlMainMenu">
    <children>
        <AnchorPane fx:id="menu">
            <VBox spacing="8"
                  AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0"
                  AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
                <Button text="click" onAction="#click"/>
            </VBox>
        </AnchorPane>
    </children>
</fx:root>

Change Scene:

@FXML protected void click(ActionEvent event) throws Exception{
    FXMLLoader fxmlLoader = new FXMLLoader();
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/Options.fxml").openStream());
    Stage stage = (Stage)menu.getScene().getWindow();
    stage.setScene(new Scene(p));
    stage.show();
}

overlap:

@FXML protected void Options(ActionEvent event) throws Exception{
    FXMLLoader fxmlLoader = new FXMLLoader();
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/Options.fxml").openStream());
    menu.getChildren().add(p);
}

fxml (old one did not have the fx:root line on it):

<fx:root type="javafx.scene.layout.AnchorPane" xmlns:fx="http://javafx.com/fxml"
         fx:controller="controller.CtrlOptions" stylesheets="view/Style.css">
    <children>
        <GridPane xmlns:fx="http://javafx.com/fxml" alignment="center"
                  hgap="10" vgap="10" id="optionBackgorund"
                  AnchorPane.topAnchor="50.0" AnchorPane.bottomAnchor="50.0"
                  AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0"
                  fx:id="options">
            <!-- the view -->
        </GridPane>
    </children>
</fx:root>

ok, now can send an object and making a scene with fx:root, but still have the problem of the scene not fitting the stage

and now have a new problem, I did not have without fx:root, and this one was the one of using another AnchorPane to overlap the current one, now doesn't overlap it, it just appears in the middle but the size is kept small (before fitted with the anchor)

Author:user1090694,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14938945/scene-not-fitting-in-the-stage
yy