Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX Changing Scene Instead of Stage

I'm currently making a program and using JavaFX as the main GUI setting. Currently I'm opening and closing each stage as I go into each different method.

This is a little bit annoying as to the user there are a lot of screens opening and closing when they go through the program.

My question is this, is there a way to instead of opening a stage every time I can move into a different part of my program, I could switch between scenes instead? Would switching between scenes be the correct implementation in this scenario? Would I need to pass a stage between methods?

As you can probably tell I'm just getting started with JavaFX so would appreciate a bit of a lesson.

EDIT: I have decided to update my application using JavaFXML, However I am having difficulties passing a scene into my controller. Here is my Controller;

public class MainApp extends Application {

    @FXML
    public Stage primaryStage;

    @FXML
    private AnchorPane rootLayout;

    @FXML
    private JobInterface jInterface;

    @Override
    public void start(Stage primaryStage) {
        primaryStage = new Stage();
        setPrimaryStage(primaryStage);
        initRootLayout();
    }

    @FXML
    public void initRootLayout(){
        try {
            primaryStage = getPrimaryStage();
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("MainInterface.fxml"));
            rootLayout = (AnchorPane) loader.load();        
            Scene scene = new Scene(rootLayout);    
            primaryStage.setScene(scene);
            primaryStage.show();
            setPrimaryStage(primaryStage);
         } catch (IOException e) {
                e.printStackTrace();
         }
    }

    @FXML
    private void setJobLayout(){
        primaryStage = getPrimaryStage();
        jInterface = new JobInterface();
        jInterface.initJobLayout();
        primaryStage.setScene(jInterface.getScene());
    }

    public static void main(String[] args) {
        launch(args);
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public void setPrimaryStage(Stage primaryStage) {
        this.primaryStage = primaryStage;
    }
}

Here is a method that is changing the scene using a different FXML file and attempting to pass the scene back to the controller;

public class JobInterface {

    private AnchorPane rootLayout;
    private Scene scene;

    public void initJobLayout(){
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("JobInterface.fxml"));
            rootLayout = (AnchorPane) loader.load();
            scene = new Scene(rootLayout);
            setScene(scene);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Scene getScene() {
        return scene;
    }

    public void setScene(Scene scene) {
        this.scene = scene;
    }   
}

The issue I'm having now is a NullPointerException on this line in the main app;

primaryStage.setScene(jInterface.getScene());

Any ideas on where I am going wrong?

like image 598
jbanks Avatar asked Oct 31 '22 20:10

jbanks


1 Answers

Non-Animated

For changing scene without Animation tutorials: http://docs.oracle.com/javafx/2/api/javafx/stage/Stage.html#setScene%28javafx.scene.Scene%29 http://docs.oracle.com/javafx/2/api/javafx/scene/layout/Pane.html#getChildren%28%29

Animated

For changing scene with Animation tutorials: https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx1 https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx

like image 109
Programmer Avatar answered Nov 15 '22 05:11

Programmer