2

I would like an explanation as to why some white space appears around my content when I set the stage's re-sizable property to false. I want to prevent the user from resizing the window, but keep the window so that there is no white space showing.

Things I have tried: I have tried setting the min and max height of every element, pane, grid, stage, etc... I have tried setting the margins and paddings and insets to zero. I also tried something weird I read online that involved setting the insets to -1. Some of these work, but they involve seemingly random values needing to be added or subtracted from the GAME_HEIGHT and GAME_WIDTH. For example, setting

Scene scene = new Scene(pane, GAME_WIDTH-12, GAME_HEIGHT-12);

works but seems too arbitrary and it feels wrong to hard-code random 12's in.

Here is the minimum code needed to recreate what is happening:

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

@Override
public void start(Stage primaryStage) throws Exception {
    final int GAME_HEIGHT = 480;
    final int GAME_WIDTH = 736;
    Image img = new Image("file:test.gif");
    GridPane grid = new GridPane();
    Pane pane = new Pane();

    for(int row=0; row<15; row++)
        for(int column=0; column<23; column++) {
            ImageView view = new ImageView(img);
            view.setViewport(new Rectangle2D(0, 0, 32, 32));
            grid.add(view, column, row);
        }
    // Imageview viewports are 32x32
    // 15*32 = 480 = GAME_HEIGHT 
    // 23*32 = 736 = GAME_WIDTH 

    pane.getChildren().addAll(grid);
    Scene scene = new Scene(pane, GAME_WIDTH, GAME_HEIGHT);
    primaryStage.setScene(scene);
    //primaryStage.setResizable(false); // commenting this out makes the game window the correct size
    primaryStage.show();
}

1. Before setting the stage resizable to false. 2. After setting resizable white space appears on the right and bottom. 3. After setting the stages max height and width, it zoom cuts the content off. Before setting the stage resizable to false

After setting resizable white space appears on the right and bottom

After setting the stages max height and width, it zoom cuts the content off

Kyo
  • 21
  • 2
  • 1
    Does [`Window#sizeToScene()`](https://openjfx.io/javadoc/14/javafx.graphics/javafx/stage/Window.html#sizeToScene()) help at all? – Slaw May 12 '20 at 15:06
  • Use a public resource like `Image img = new Image("https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/32x32/Circle_Green.png");` to demonstrate the problem and post an image of what you get. – c0der May 12 '20 at 16:00
  • @Slaw That did resolve it yes. Although, sizeToScene() does not show up as an available method when I scroll the list in eclipse. It works but there is not context menu. Sorry for the noob question. I can usually manage on my own, but this one I couldn't figure out. – Kyo May 13 '20 at 07:26

0 Answers0