Navigation between JavaFX screens

2

What is the best way to navigate from one screen to another using JavaFx. The way I'm doing every time the first screen calls the second screen the second screen opens with the size of the first one.

This is how I call the second screen:

Parent root = FXMLLoader.load(getClass().getResource("frmPegaXml.fxml"));

SistemaDemonstrativos.SCENE.setRoot(root); 

This is my Main:

public static Scene SCENE;

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("frmLogin.fxml"));

        Scene scene = new Scene(root);
        SCENE = scene;

        setUserAgentStylesheet(STYLESHEET_MODENA);
        stage.setResizable(false);

        //scene.getStylesheets().add("css/JMetroLightTheme.css");
        stage.centerOnScreen();
        stage.getIcons().add(new javafx.scene.image.Image("icons/1432842939_chart-icon-tm.png"));

        stage.setTitle("Titulo");
        stage.setScene(scene);
        stage.show();
    }
    
asked by anonymous 28.05.2015 / 21:05

1 answer

0

I do not understand much what you want. Do you want to navigate between screens, or just create standalone screens? Take a look at this:

public class Main extends Application{ 
    public static Stage WINDOWS;
    public static Scene SCENE_1, SCENE_2;

    public Parent createContent_1(){
        // Conteúdo I
        StackPane root = new StackPane();
        root.setPrefSize(500, 500);

        Button btn_1 = new Button("Cenário_1");
        btn_1.setOnAction(e -> WINDOWS.setScene(SCENE_2));
        root.getChildren().addAll( btn_1);

        return root;
    }

    public Parent createContent_2(){

        // Conteúdo II
        StackPane root = new StackPane();
        root.setPrefSize(500, 500);
        Button btn_2 = new Button("Cenário_2");
        btn_2.setOnAction(e -> WINDOWS.setScene(SCENE_1));
        root.getChildren().add(btn_2);

        return root;
    }

    @Override
    public void start(Stage primaryStage) throws Exception{

        WINDOWS = primaryStage;

        SCENE_1 = new Scene(createContent_1());
        SCENE_2 = new Scene(createContent_2());

        WINDOWS.setScene(SCENE_1);

        WINDOWS.setTitle("Teclado Fonêmico");

        WINDOWS.show();
    }  
    public static void main(String [] args){launch(args);}
}
    
29.05.2015 / 20:55