23

I am trying to create a splash screen like the example I've provded. It seems that AnchorPane does not allow transparent background, I've tried setting the css of the AnchorPane to -fx-background-color: rgba(255,0,255,0.1) ; but the white background still shows up.

All I have in my fxml file is a AnchorPane with ImageView with contain the png image

Example

I've looked everywhere but can't find any solution, any help would be appreciated. Thanks

francisOpt
  • 860
  • 1
  • 8
  • 15

2 Answers2

26

Try this JavaFX splash sample created for the Stackoverflow question: Designing a splash screen (java). And a follow up sample which also provides application initialization progress feedback.

JavaFX does offer the Preloader interface for smooth transfer from splash to application, but the above samples don't make use of it.

The splash samples above also don't do the transparent effect, but this dialog sample shows you how to do that and you can combine it with the previous splash samples to get the effect you want.

The transparent effect is created by:

  1. stage.initStyle(StageStyle.TRANSPARENT).
  2. scene.setFill(Color.TRANSPARENT).
  3. Ensuring your root node is not an opaque square rectangle.

Which is all demonstrated in Sergey's sample.

Related question:

Update Apr 2016 based on additional questions

the preloader image isnt in the foreground. I have tried stage.toFront(), but doesnt help.

A new API was created in Java 8u20 stage.setAlwaysOnTop(true). I updated the linked sample to use this on the initial splash screen, which helps aid in a smoother transition to the main screen.

For Java8+

For modena.css (the default JavaFX look and feel definition in Java 8), a slight shaded background was introduced for all controls (and also to panes if a control is loaded).

You can remove this by specifying that the default background is transparent. This can be done by adding the following line to your application's CSS file:

.root { -fx-background-color: transparent; }

If you wish, you can use CSS style classes and rules or a setStyle call (as demonstrated in Sergey's answer) to ensure that the setting only applies to the root of your splash screen rather than all of your app screens.

See related:

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Thanks guys! Both answer are good but `scene.setFill(Color.TRANSPARENT).` you don't need to specify the width and height of the scene – francisOpt Feb 20 '13 at 20:04
  • I was experiencing a milisecond-flick with background visible when the stage was shown. I managed to solve this issue by calling: `root.setCache(true); root.setCacheHint(CacheHint.SPEED);` _(where root is Parent instance)_. – stuchl4n3k Sep 04 '14 at 16:50
  • When I use stage.initStyle(Transparent), the preloader image isnt in the foreground. I have tried stage.toFront(), but doesnt help. If I switch back to undecorated, it works fine. Any ideas? – Darth Ninja Apr 22 '16 at 03:51
  • Unfortunately, that didn't work. I'm using Java 8u66. – Darth Ninja Apr 23 '16 at 13:13
14

You need to have transparent Stage and Scene for that. Pane itself doesn't have a color.

public void start(Stage primaryStage) {
    Button btn = new Button("Say 'Hello World'");

    AnchorPane root = new AnchorPane();
    root.getChildren().add(btn);

    // Java 8: requires setting the layout pane background style to transparent
    // https://bugs.openjdk.java.net/browse/JDK-8092764
    // "Modena uses a non-transparent background by default"
    root.setStyle("-fx-background-color: transparent;"); 

    Scene scene = new Scene(root, 300, 250, Color.TRANSPARENT);
    primaryStage.initStyle(StageStyle.TRANSPARENT);
    primaryStage.setScene(scene);
    primaryStage.show();
}
jewelsea
  • 150,031
  • 14
  • 366
  • 406
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • how will i unit test this. I simply tried to unit test this but it shows not on fx application thread error. – vinay Mar 24 '15 at 09:42
  • @vinay take a look at next questions: http://stackoverflow.com/questions/11385604/how-do-you-unit-test-a-javafx-controller-with-junit and http://stackoverflow.com/questions/10599724/gui-testing-framework-for-javafx-2/10600676 – Sergey Grinev Mar 24 '15 at 13:56