3
votes

I want my Javafx FXML application to start maximized so I used the method setMaximized(true) in my stage.

The program opens as Maximized no problem on that, but the problem is that there is a small black area that flashes for half a second on the application startup just before the window appears.

Here's a recording (gif) of what I describe: enter image description here

I figured out that the problem is with the scene as it is trying to open in its prefWidth & prefHeight then it scales up to fit the stage. How can I fix this and make the program start as normal programs do?

here's my start() method:

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("editor.fxml"));
    primaryStage.setTitle("Simple Text Editor");
    primaryStage.setScene((new Scene(root)));
    primaryStage.setMaximized(true);
    primaryStage.show();
}
1
What happens if you build a release (.exe. / .jar) and run it? If the glitch is still shown try calling "primaryStage.show()" in new thread (e.g. Platform.runLater()) - Martin Pfeffer
yes, the same thing happens when running a jar. also calling "primaryStage.show()" in "Platform.runLater()" didn't change anything. - user5946312
Note: adding primaryStage.setResizable(false);, instead of the white and black rectangles, it shows a small window that suddenly get maximized. - Linuslabo

1 Answers

3
votes

The only workaround I found is this:

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("editor.fxml"));
    primaryStage.setTitle("Simple Text Editor");
    primaryStage.setScene(new Scene(root));
    primaryStage.setMinWidth(450);
    primaryStage.setMinHeight(300);

    Screen screen = Screen.getPrimary();
    Rectangle2D bounds = screen.getVisualBounds();
    primaryStage.setWidth(bounds.getWidth());
    primaryStage.setHeight(bounds.getHeight());

    primaryStage.setMaximized(true);
    primaryStage.show();
}