Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

how to make transparent scene and stage in javafx?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...