This discussion is archived
2 Replies Latest reply: Feb 25, 2013 2:36 AM by 984080 RSS

scene not fitting in the stage

990211 Newbie
Currently Being Moderated
I have the problem that when changing the scene it gets reduced to the smallest possible on top left corner of the stage

loading the stage:
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();
}
changing 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>
then i was told to change to fx:root, so now i have the following:
loading the stage:
public void start(Stage stage) throws Exception{
    FXMLLoader fxmlLoader = new FXMLLoader();
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/MainMenu.fxml").openStream());
    stage.setScene(new Scene(p));

    stage.setWidth(1080);
    stage.setHeight(720);
    stage.setFullScreen(false);
    stage.show();
}
changing scene:
@FXML protected void click(ActionEvent event) throws Exception{
    FXMLLoader fxmlLoader = new FXMLLoader();
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/MainMenu.fxml").openStream());
    Stage stage = (Stage)menu.getScene().getWindow();
    stage.setScene(new Scene(p));
    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>
But the problem is still there, even more, now i have one more problem, cant overlap a pane with another one, the anchoring not working and ends up in middle of the pane and rearranging the elements to not superpose any of them.
@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:
<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>
I was also told to check netbeans sample codes, but the only codes i found there were not with fxml and on the net i have not been able to find any example of changing scenes

I would be very grateful is somebody could help me with this issue

Edited by: 987208 on Feb 21, 2013 4:59 AM
  • 1. Re: scene not fitting in the stage
    990211 Newbie
    Currently Being Moderated
    can someone help me please?

    else where can i find a tutorial of fx:root? have not found any at all

    or any tutorial of changing scenes?

    EDIT: found a way to do it, but don't really like it:
    with <Scene><AnchorPane> and sending the anchor pane to all the controllers ill need to show

    Edited by: 987208 on Feb 25, 2013 2:05 AM
  • 2. Re: scene not fitting in the stage
    984080 Explorer
    Currently Being Moderated
    There are several possible solutions for switching screens.
    I would suggest you to set 1 scene, with 1 parent object. (exaple StackPane).

    When you want to swith screens, remove all childrens from your stackpane & replace them by your new childrens. (those childrens can come from a .fxml file)

    You can find a small example here http://stackoverflow.com/questions/11563298/how-to-change-sub-fxml-gui-parts-at-runtime-with-button-click

    here more info about the scene: http://docs.oracle.com/javafx/2/scenegraph/jfxpub-scenegraph.htm

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points