JavaFX New Scene on Button Click

user6904864 picture user6904864 · Mar 16, 2017 · Viewed 24.7k times · Source

The title may be a bit vague, so allow me to define it a little better. I have a working piece of code (down below): a simple main menu for a game I am working on. Everything works well, except for the Start button.

What I want to be able to do is click the Start button, and have a new scene appear on the same stage (window). I do not want to see a new window open. I have talked with someone more experienced in Java, and they told me to create separate classes for the MenuFX and the GameFX. If that is the case, I would need to call some start or launch method on the GameFX class from the MenuFX class, correct? Is this the best approach, or would I want to keep all FX-related code in one class? Also, I should keep the same stage for all FX work, no?

This post shed some light on things, but I am not well-versed in some of the terms discussed- for instance, I still do not understand the concept of Root.

Also, this post talks about a similar application, but I am not using FXML or SceneBuilder... I do not know if any of it is relatable.

MenuFX.java - I have removed some of the working code, simply for brevity. You can see that all I need help with is tying the Start button to some functionality that makes a new empty scene.

/* 
 * This is simply working on the title screen.
 */

// Asssume all imports are correct
import java.everythingNeeded


public class MenuFX extends Application {
@Override

        public void start (Stage primaryStage) {

        // Make the window a set size...
        primaryStage.setResizable(false);


        // Create menu vbox and set the background image
        VBox menuVBox = new VBox(30);
        menuVBox.setBackground(new Background(new BackgroundImage(new 
        Image("image/bambooBG.jpg"), null, null, null, new BackgroundSize(45, 
        45, true, true, true, true))));



        // Create start button
        Button startButton = new Button("Start Game");

        // TODO Some things...
        // Need assistance here



        // Create help button
        Button helpButton = new Button("Help");
        helpButton.setOnAction(e -> THINGS);

        // Create music toggle button
        ToggleButton musicButton = new ToggleButton("Music On/Off");
        musicButton.setOnAction(e -> THINGS);

        // Create credits button
        Button creditsButton = new Button("Credits");
        creditsButton.setOnAction(THINGS);

        // Create exit button and set it to close the program when clicked
        Button endButton = new Button("Exit Game");
        endButton.setOnAction(e -> Platform.exit());

        // Add all nodes to the vbox pane and center it all
        // Must be in order from top to bottom
        menuVBox.getChildren().addAll(startButton, helpButton, musicButton, creditsButton, endButton);
        menuVBox.setAlignment(Pos.CENTER);

        // New scene, place pane in it
        Scene scene = new Scene(menuVBox, 630, 730);

        // Place scene in stage
        primaryStage.setTitle("-tiles-"); 
        primaryStage.setScene(scene); 
        primaryStage.show(); 
    }


    // Needed to run JavaFX w/o the use of the command line
    public static void main(String[] args) {

        launch(args);
    }

}

Restating: I want to click the Start button and have the currently open window change to an empty scene.

Here is a pastebin of the MenuFX class in its entirety: http://pastebin.com/n6XbQfhc

Thank you for any help,

Bagger

Answer

James_D picture James_D · Mar 16, 2017

The basic idea here is you would do something like:

public class GameFX {

    private final BorderPane rootPane ; // or any other kind of pane, or  Group...

    public GameFX() {

        rootPane = new BorderPane();

        // build UI, register event handlers, etc etc

    }

    public Pane getRootPane() {
        return rootPane ;
    }

    // other methods you may need to access, etc...

}

Now back in the MenuFX class you would do

Button startButton = new Button("Start Game");
startButton.setOnAction(e -> {
    GameFX game = new GameFX();
    primaryStage.getScene().setRoot(game.getRootPane());
});