I had a hard time figuring out why my transparent stage refuses to be transparent. Finally I found out, that it was caused by a Tooltip
that was installed into an ImageView
:
ImageView imageViewIcon = new ImageView();
imageViewIcon.setFitWidth(70);
imageViewIcon.setFitHeight(70);
imageViewIcon.setPreserveRatio(false);
imageViewIcon.setImage(new Image("./next.png"));
Tooltip tooltip = new Tooltip("Tooltip!");
if (this.config.getShowTooltip("true")) {
Tooltip.install(imageViewIcon, tooltip);
}
When I comment out the last 4 lines, the transparency works as expected, but with the Tooltip
installed the stages background is grayish (e.g. the default window background). Though it's obvious what the button does and the tooltip is not essential for my layout it'd be nice to have, just to give a little hint...
Any suggestions or workarounds?
Solution
Set the style
-fx-background-color: transparent
on the root node of the scene.Background
Similar behavior is discussed in an Oracle JavaFX Forum post on the JavaFX Scene/Fill Color.
Relevant comments from the thread by David Grieve, the lead developer for the JavaFX CSS features:
In the case of your question,
Tooltip
is a control, so when you add it to the scene it implicitly triggers the defaultmodena.css
stylesheet to be loaded for the scene (which sets the background of the root node of the scene to gray rather than a null or transparent fill which is used when there are no controls in the scene). To retain the transparent background for the application when a control is used in the scene, it is necessary to explicitly set the scene root node background to transparent.Sample code for a transparent stage: