Search code examples
javaimageviewscene

Java - Scene1 not showing, identical Scene2 works


I am having a hard time seeing why this code does not work. No compilation errors.

scene2 is drawn just as I expect it to. The buttons in both scenes work. Scene1 will not show the images. While it is identical to scene2 that does work. The issue seems to be with scene1.

if I change this line

primaryStage.setScene(scene1);

to

primaryStage.setScene(scene2);

Scene2 again works, but scene1 is broken, with only the button showing. Again this seems to point that scene1 object is the issue???

Any help pointing out the obvious would be appreciated.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;

import java.util.Scanner;


public class Main extends Application {

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

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

        Button button = new Button();
        Button button1 = new Button();

        button.setText("Start Games");
        button1.setText("Quit game");

        Image displayedimg = new Image("old_four_finger.png");
        Image displayedimg2 = new Image("edelen_tree_srv.png");

        ImageView iv1 = new ImageView();
        iv1.setImage(displayedimg);

        ImageView iv2 = new ImageView();
        iv2.setImage(displayedimg2);

        Group root1 = new Group();
        HBox layout1 = new HBox();
        Scene scene1 = new Scene(root1);
        layout1.setStyle("-fx-background-color: #00ff00;");
        layout1.getChildren().add(button);
        layout1.getChildren().add(iv1);
        layout1.getChildren().add(iv2);
        root1.getChildren().add(layout1);

        Group root2 = new Group();
        HBox layout2 = new HBox();
        Scene scene2 = new Scene(root2);
        layout2.setStyle("-fx-background-color: #00ff00;");
        layout2.getChildren().add(button1);
        layout2.getChildren().add(iv1);
        layout2.getChildren().add(iv2);
        root2.getChildren().add(layout2);

        // Set our button(s) clickable actions
        button.setOnAction(e -> {
            primaryStage.setScene(scene2);
        });
        button1.setOnAction(e -> {
            primaryStage.setScene(scene1);
        });

        primaryStage.setScene(scene1);
        primaryStage.show();
    }
}

Solution

  • An element (in this case the ImageViews) can only have 1 parent. When you use

    layout1.getChildren().add(iv1);
    

    then you set iv1's parent to layout1. But when you do

    layout2.getChildren().add(iv2);
    

    then you set iv1's parent to layout2, thus removing it from layout1. Try setting the parent if iv1 dynamically based on which scene is used.