How to set pop up background Transparent Java Fx

Viewed 781

I am trying to show a success message gif from my javaFx application.

public static void successGif() {
    int size = 400;
    ImageView splash = new ImageView(new Image("file:src/main/resources/img/success3.gif"));
    splash.setStyle("-fx-background-color: transparent;");
    splash.setFitWidth(size);
    splash.setFitHeight(size);
    splash.setPickOnBounds(true);
    Pane splashLayout = new Pane();
    splashLayout.getChildren().add(splash);
    final Stage initStage = new Stage();
    Scene successScene = new Scene(splashLayout, size, size);
    successScene.setFill(Color.TRANSPARENT);
    initStage.initStyle(StageStyle.TRANSPARENT);
    initStage.setWidth(size);
    initStage.setHeight(size);
    initStage.setScene(successScene);
    initStage.show();
    new Thread(() -> {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Platform.runLater(initStage::close);

    }).start();
}

The above function can create a popup with a gif successfully. for now, I tried calling this function through a button in my application and the background of the popup is still white. But when I tested the same function by calling it in the start() function of the application, it works as expected. how can I fix this issue?

I need to call this function inside my application on many occasions.

How to make the background transparent.

The following is the gif in case you need to try, Thanks.

enter image description here

1 Answers

You can use a javafx.scene.Group instead of adding splash layout directly to the scene, You can change your void method successGif() to,

public static Stage successGif() {
    int size = 600;
    ImageView splash = new ImageView(new
    Image("file:src/main/resources/img/success3.gif"));
    splash.setStyle("-fx-background-color: transparent;");
    splash.setFitWidth(size);
    splash.setFitHeight(size);
    splash.setPickOnBounds(true);
    Pane splashLayout = new Pane();
    splashLayout.getChildren().add(splash);
    final Stage initStage = new Stage();
    Group group = new Group();
    group.getChildren().add(splashLayout);
    //        group.setStyle("-fx-background-color: transparent");
    Scene successScene = new Scene(group, size, size);
    successScene.setFill(Color.TRANSPARENT);
    initStage.initStyle(StageStyle.TRANSPARENT);
    initStage.setWidth(size);
    initStage.setHeight(size);
    initStage.setScene(successScene);
    initStage.setAlwaysOnTop(true);
    initStage.show();
    return initStage;
}

and then if you want the Stage object, then you can just get the returned object from the method

Related