1

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)

user1090694
  • 609
  • 4
  • 8
  • 16
  • your example doesn't work for me (clicking on the button does nothing) – zhujik Feb 18 '13 at 15:41
  • I suggest not to alter the scene but replace its root whenever the screen is going to be changed. The FXMLs' most top paret will be a layout container with controller class attached. To send an object to the controller, first get the controller instance after the FXML has been loaded then call methods you have written in th controller to update the view. – Uluk Biy Feb 18 '13 at 18:49
  • can put me an example of replacing root and sending an object to the controller, please?, haven't found any... (or keywords used on search were not the correct ones...) – user1090694 Feb 18 '13 at 22:59
  • Please see [http://stackoverflow.com/q/10751271/682495](http://stackoverflow.com/q/10751271/682495), [http://stackoverflow.com/q/9717852/682495](http://stackoverflow.com/q/9717852/682495) and related Q&As here on SO. – Uluk Biy Feb 19 '13 at 09:03
  • thanks, the first link will be useful to send objects to the controller. and the fx:root still don't really understand how it works, will have to search more information about it (or an example on changing scenes with it) – user1090694 Feb 19 '13 at 13:51
  • Have not been able to find examples using fx:root, can help me with it please? i edited the post with a sample code from how i have it – user1090694 Feb 19 '13 at 17:34
  • fx:root is more appropriate for creating reusable custom components see [Creating a Custom Control with FXML](http://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm#BABDAAHE). For a regular example use Netbeans, in there create a new FXML project and examine the code of loading fxml file, the same logic is applicable for loading different more fxml files. Or create a project from a sample apps (Login sample app for example) bundled with Netbeans. – Uluk Biy Feb 19 '13 at 22:03
  • my netbeans don't have fxml samples and i have not been able to find any example of changing scenes with fxml... now have been able to use the fx:root, but new problems appeared (and old ones still there) – user1090694 Feb 20 '13 at 13:56
  • still have no idea on how to do it, only way im thinking that could work is doing another fxml with and then, instead of changing things of the scene or the stage, change them from the AnchorPane, and then don't use fx:root, since i have no idea on how to use that nor have found any examples (neither from the other way) of changing scenes on a stage – user1090694 Feb 22 '13 at 13:47
  • is the ONLY way to do this with that and sending the AnchorPane to all the controllers I need? don't really lke to do it this way... – user1090694 Feb 25 '13 at 10:08

1 Answers1

0

found the way to fit the scene to the stage:

@FXML protected void click(ActionEvent event) throws Exception{
    FXMLLoader fxmlLoader = new FXMLLoader();
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/Options.fxml").openStream());
    Scene scene= menu.getScene();
    scene.setRoot(p);
}
user1090694
  • 609
  • 4
  • 8
  • 16