Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start the application window maximized in JavaFX FXML not working properly

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 Answers

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();
}