Navigation between Javafx screens

Asked

Viewed 4,316 times

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.

That’s what 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();
    }

1 answer

0


I don’t really understand what you want. Do you want to navigate between screens, or just create independent screens? Take a look at this form:

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);}
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.