0

I am trying to get a background image to load in a javaFX scene. The answers I have found on here are not working. The window opens, but it is blank (no image).

public class FirstFX extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        StackPane root = new StackPane();
        BackgroundImage myBI= new BackgroundImage(new Image("SnLBoard.png"),
            BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
        root.setBackground(new Background(myBI));
        stage.show();
    }
}
jkdev
  • 11,360
  • 15
  • 54
  • 77

1 Answers1

1

You haven't associated the root to anything, so nothing is showing within the stage:

StackPane root = new StackPane();
Scene scene = new Scene(root, 640, 480);
... //Display code and logic
stage.setScene(scene);
stage.show();

You might also consider setting the min/preferred/max size of your pane. After this code, you'll want to start a thread that begins your application logic.

Zircon
  • 4,677
  • 15
  • 32