How to make a transparent scene and stage in javafx?

I want to have a transparent indicator of progress, which is uncertain.

here is the code, it shows the gray state of the background / scene. I wanted completely transparent.

I tried the following code, but it shows a background stage that 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

+8
source share
3 answers

For modena.css(defining the default appearance of JavaFX in Java 8), a small shaded background was introduced for all controls (and also for panels if the 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 CSS file:

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

, , .

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

: (initStage) start. , , initStage.

+20

stage.initStyle(StageStyle.TRANSPARENT);
(, )

scene.setFill(Color.TRANSPARENT);
( TRANSPARENT GREEN YELLOW RED BLUE...)
, ,

-

 primaryStage.setOpacity(0.2);


0,2 0 1 | 0 1 - , , - , , .

 primaryStage.setFullScreen(true);<br>

css

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

rgba (0,0,0,1)

+3

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();

You just need basically 2 things:

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

Source: https://habr.com/ru/post/1618209/


All Articles