Search code examples
javafxjavafx-2

Javafx remove quick white screen on app start


Using javafx 7 update 60. OS: Windows 7.

I've just taken helloworld from there http://docs.oracle.com/javafx/2/get_started/hello_world.htm

and changed background of root pane to red.

package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class HelloWorld extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.setStyle("-fx-background-color: #ff0000;");
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

And while window is showing up, I can see its background is white, and after a moment switches to red. How can I remove this white screen ?


Solution

  • Okay so what you're seeing is the actual stage background. The primaryStage has it's own white background which you are seeing a split second before the scene is loaded. There's no real way around this unless you set the stage's background to transparent but that also makes the entire stage transparent including the exit button so you have to have a way for the user to exit your program.