0
votes

The example below works for the Text only but once I add a button to the stage, the transparent become inactive

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        stage.initStyle(StageStyle.TRANSPARENT);
        Text text = new Text("!");
        text.setFont(new Font(40));
        VBox box = new VBox();
        Button btn = new Button("Test transparent");
        box.getChildren().addAll(text, btn);
        //if I removed the btn, transparent works as expected. 
        final Scene scene = new Scene(box,300, 250);
        scene.setFill(null);
        stage.setScene(scene);
        stage.show();
    }

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

What I am looking for is to only make the stage transparent but show the text and button

1

1 Answers

1
votes

Your Button in that case is not an issue. By default VBox has gray background, so your Stage is transparent, but VBox isn't. You have to set transparent background by CSS file or inline or from code:

CSS:

.your-vbox {
    -fx-background-color: transparent;
}

Inline:

box.setStyle("-fx-background-color: transparent;");

Code:

box.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY)));