Steven Matos
Steven Matos

Reputation: 33

How can I get an image to show from another window in javafx?

I want to allow the user to proceed to the next window by using a button. This button then shows an image. However, when I tried to run the program I get an exception thrown. Here is my code:

package matchingcards;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class MatchingCards extends Application {

    Stage window;
    Scene start, game;

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

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

        // When user clicks start, program enters game
        Button btn = new Button("Start Game");
        btn.setOnAction(e -> window.setScene(game));

        // Create the display for start button
        GridPane pane1 = new GridPane();
        pane1.getChildren().addAll(pane1, btn);
        start = new Scene(pane1, 200, 200);

        // Create display for game
        GridPane pane2 = new GridPane();
        Image back = new
                Image("https://i.pinimg.com/736x/c1/59/b4/" +
                      "c159b4738dae9c9d8d6417228024de8d--" +
                      "playing-card-design-card-card.jpg", 300, 200, false, false);
        pane2.getChildren().addAll(new ImageView(back));
        Scene game = new Scene(pane2, 500, 500);

        window.setScene(start);
        window.setTitle("Matching Cards");
        window.show();
    }
}

Upvotes: 0

Views: 967

Answers (1)

becktartam
becktartam

Reputation: 38

You are having an exception because you are adding pane1 inside itself

GridPane pane1 = new GridPane();
pane1.getChildren().addAll(pane1, btn);

I have corrected your code a bit:

package matchingcards;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class MatchingCards extends Application {

    Stage window;
    Scene start, game;

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

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

        window = primaryStage;

        // When user clicks start, program enters game
        Button btn = new Button("Start Game");

        btn.setOnAction(e -> window.setScene(game));

        // Create the display for start button
        GridPane pane1 = new GridPane();
        pane1.getChildren().addAll(btn);
        start = new Scene(pane1, 200, 200);

        // Create display for game
        GridPane pane2 = new GridPane();
        Image back = new
                Image(
                "https://i.pinimg.com/736x/c1/59/b4/c159b4738dae9c9d8d6417228024de8d--" +
                "playing - card - design - card - card.jpg",
                300, 200, false, false);
        pane2.getChildren().addAll(new ImageView(back));
        game = new Scene(pane2, 500, 500);

        window.setScene(start);
        window.setTitle("Matching Cards");
        window.show();
    }
}

Upvotes: 2

Related Questions