how to make transparent scene and stage in javafx?
Asked Answered
L

3

13

I want to have transparent progressindicator, which is indefinite.

here is the code, it shows grey background state/scene. i wanted fully transparent.

I tried following code, but it shows background stage which is not transparent.

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

    public class Main extends Application {

        @Override
        public void start(Stage stage) {
            /*
             * 
             * my css file content:
             * 
             * .progress-indicator .indicator { -fx-background-color: transparent;
             * -fx-background-insets: 0; -fx-background-radius: 0;
             * 
             * } .progress-indicator { -fx-progress-color: green ; }
             * 
             * 
             * 
             */
            Stage initStage = new Stage();

            initStage.initStyle(StageStyle.TRANSPARENT);
            ProgressIndicator loadProgress = new ProgressIndicator();
            loadProgress.setSkin(null);
            loadProgress.setPrefWidth(50);
            VBox box = new VBox();
            box.getChildren().add(loadProgress);
            final Scene scene = new Scene(box, 150, 150);

            scene.setFill(Color.TRANSPARENT);

            initStage.setScene(scene);
            scene.getStylesheets().add("application.css");

            initStage.show();

        }

        public static void main(String[] args) {
            launch(args);
        }

    }

output

Labroid answered 2/12, 2015 at 0:58 Comment(1)
Related to: javafx transparent window background with decorations.Dose
D
28

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

This is in addition to other settings you already have in your code to initialize the style of the stage and background fill of the scene.

stage.initStyle(StageStyle.TRANSPARENT);
scene.setFill(Color.TRANSPARENT);

Note: in the questions's sample code, an additional stage (initStage) is created instead of using the passed in stage for the start method. The passed in stage can be initialized, utilized and shown directly by your code rather than creating an additional initStage.

Dose answered 2/12, 2015 at 1:17 Comment(1)
Tutorials on JavaFX are way to bad at mentioning stuff like this. Feels like their code fail 50% of the time because of "suprises" like this :-(. Spent 2 hours on this until I gave up, and found your solution. Thanks mate!Multiplier
I
12
stage.initStyle(StageStyle.TRANSPARENT);

this is for hide the top bar ( minimize, Restore Down and close)

scene.setFill(Color.TRANSPARENT);

this is for the frame color ( you can replace TRANSPARENT with any color GREEN YELLOW RED BLUE ...) but for me I want glass view if you can understand me, and with different color so the solution is

primaryStage.setOpacity(0.2);

The number 0.2 is between 0 and 1. 0 is hidden and 1 is normal form but between the numbers transparent so choose your number and run your program and see if this is what you want there is this code for full screen.

primaryStage.setFullScreen(true);

and in the css file do this

.root { -fx-background-color:rgba(0,0,0,1); }

and you can change the color with changed the number in rgba(0,0,0,1)

Inman answered 24/3, 2017 at 16:57 Comment(0)
S
2

This works for me.

Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
Scene scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
stage.setScene(scene);
stage.initStyle(StageStyle.TRANSPARENT);
stage.show();

U just need mainly 2 things:

scene.setFill(Color.TRANSPARENT);
stage.initStyle(StageStyle.TRANSPARENT);
Skurnik answered 13/8, 2019 at 13:7 Comment(2)
On JavaFX 10 it doesn't work for me, scene.setFill requires a Paint not an int.Patently
@GuillaumeF. A Color derives from Paint (it is a subclass). And Color.TRANSPARENT is a Color (which is a Paint) and is not an int. So setting the fill to the transparent color is OK. This solution should work fine unless a Control is involved in the scene, in which case the additional CSS setting discussed in other answers is also required.Dose

© 2022 - 2024 — McMap. All rights reserved.