terrop
terrop

Reputation: 33

Javafx switching scenes using different panes

I have class SceneLibrary which I would like to use to switch scenes. When I run application I have < init > errors at PaneOne private SceneLibrary sceneLibrary = new SceneLibrary(); and < init > errors at SceneLirary private Scene sceneOne = new Scene(new PaneOne(),300,500);. I can not use static scenes.

public class SceneLibrary {
    private Stage primaryStage;
    private Scene sceneOne = new Scene(new PaneOne(),300,500);
    private Scene sceneTwo = new Scene(new PaneTwo(),400,500);

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

    public void switchToOne(){
        primaryStage.setScene(sceneOne);
    }

    public void switchToTwo(){
        primaryStage.setScene(sceneTwo);
    }
}

My PaneOne class:

public class PaneOne extends AnchorPane {
    private SceneLibrary sceneLibrary = new SceneLibrary();
    public PaneOne() {
        Button button = new Button();
        this.getChildren().add(button);

        button.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                sceneLibrary.switchToTwo();
            }
        });
    }
}

My Main class:

public class Main extends Application {
    private SceneLibrary sceneLibrary = new SceneLibrary();
    @Override
    public void start(Stage primaryStage) throws Exception{
        AnchorPane root = new PaneOne();
        primaryStage.setTitle("Hello World");
        sceneLibrary.setPrimaryStage(primaryStage);
        primaryStage.setScene(new Scene(root, 300, 500));
        primaryStage.show();
    }

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

Upvotes: 1

Views: 223

Answers (1)

c0der
c0der

Reputation: 18812

Share an instance of sceneLibrary:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        SceneLibrary sceneLibrary = new SceneLibrary();
        sceneLibrary.setPrimaryStage(primaryStage);
        sceneLibrary.switchToOne();//set scene one to the stage 
        primaryStage.show();
    }

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

class PaneOne extends AnchorPane {

    public PaneOne(SceneLibrary sceneLibrary) {
        Button button = new Button("Switch To Scene 2");
        this.getChildren().add(button);
        button.setOnAction(event -> sceneLibrary.switchToTwo());
    }
}

class PaneTwo extends AnchorPane {

    public PaneTwo(SceneLibrary sceneLibrary) {
        Button button = new Button("Switch To Scene 1");
        this.getChildren().add(button);
        button.setOnAction(event -> sceneLibrary.switchToOne());
    }
}

class SceneLibrary {

    private Stage primaryStage;
    private final Scene sceneOne = new Scene(new PaneOne(this),300,500);
    private final Scene sceneTwo = new Scene(new PaneTwo(this),400,500);

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

    public void switchToOne(){
        primaryStage.setScene(sceneOne);
    }

    public void switchToTwo(){
        primaryStage.setScene(sceneTwo);
    }
}

Upvotes: 1

Related Questions