V
V
vladkiriyak2017-04-02 09:24:18
JavaFX
vladkiriyak, 2017-04-02 09:24:18

How to change the scene in javaFX, through the controller?

package sample;


import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.*;

public class Main extends Application {




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

        primaryStage.setTitle("Hallo");

        Parent menuRoot = FXMLLoader.load(getClass().getResource("menuScene.fxml"));
        Parent firstRoot = FXMLLoader.load(getClass().getResource("firstScene.fxml"));


        Scene scene = new Scene(menuRoot, 600, 400);





        primaryStage.setMinHeight(600);
        primaryStage.setMinWidth(400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException {launch(args);}
}

I need that when I click on a certain button in the Stage, the scene changes, but I don’t know how to implement it. I searched for an answer on the Internet for a long time and did not find anything.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2017-04-02
@jamakasi666

Make primaryStage static, from the controller, pull primaryStage.setScene(desired scene);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question