Is it possible that JavaFX has a scene, scene, and control that are all transparent?

I have a custom control that consists of 3 simple buttons. The control handles operations for the application to which it is bound, but it must be at the external stage for reasons.

The thing that annoys him is that there is a white background between all the buttons.

I tried all this to fix it:

  • Init Stage Style to Transparent
  • Declare the scene as a transparent background: new Scene(Node, X, Y, Color.TRANSPARENT);
  • Declare a scene using a NULL background: new Scene(Node, X, Y, null);
  • Have a control (which extends HBox) .setStyle("-fx-background-color : rgba(0, 0, 0, 0);");

All of them could not eliminate the appearance of the background. I did this using Windows Forms, but is this possible with JavaFX?

0
source share
1 answer

You need to set transparency

  • stage
  • scene
  • root class (or in my case hbox, I prefer root)

This works for me:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {

            HBox hbox = new HBox();
            hbox.setSpacing(12);

            Button button1 = new Button("Button 1");
            Button button2 = new Button("Button 2");
            Button button3 = new Button("Button 3");

            hbox.getChildren().addAll(button1, button2, button3);

            Scene scene = new Scene(hbox, Color.TRANSPARENT);

//          scene.getStylesheets().add( getClass().getResource("application.css").toExternalForm());
            scene.getRoot().setStyle("-fx-background-color: transparent");

            primaryStage.initStyle(StageStyle.TRANSPARENT);
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

This is a direct solution. Of course, instead of directly customizing the style, it is better to use the application.css stylesheet:

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

Here is a screenshot with the caption above the code:

enter image description here

+1
source

All Articles