Create a Transparent Scene in JavaFX

MD Aminul Islam Jun 13, 2022
  1. Create a Transparent Scene in JavaFX
  2. An Alternative Way to Make a Transparent Stage in JavaFX
Create a Transparent Scene in JavaFX

Sometimes we see a total transparent preloader screen on an application startup. Also, we can’t see any title bar on the preloader screen that contains the default close, minimize, or maximize option.

This article will show how we can create this scene on our application. Also, we see an example with a proper explanation to make this topic easier to understand.

Create a Transparent Scene in JavaFX

In our example below, we will make an entirely invisible scene. We will see only the contents of the scene.

The code for our example will look like the below,

// Importing necessary packages.

import javafx.application.Application;
import javafx.scene.Scene;
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 FXTransparent extends Application {

    public void start(Stage PrimaryStage) {
        PrimaryStage.initStyle(StageStyle.TRANSPARENT); // Set the initial style to the stage to transparent
        Text txt = new Text("I'm totally \n transparent..."); // Creating a text element.
        txt.setFont(new Font(30)); // Set the font size to 30 for the text.
        VBox vbox = new VBox(); // Create a Vbox
        vbox.getChildren().add(txt); // Add the text to the Vbox
        Scene scene = new Scene(vbox,400, 250); // Create a scene with Vbox and dimension
        scene.setFill(null); // Set the scene to no fill
        PrimaryStage.setScene(scene); // Add the scene to stage
        PrimaryStage.show(); // Visualize the stage
    }

    public static void main(String[] args) {
        launch(args); // Launch the application
    }
}

We already commanded the purpose of each line on the code. Now, we discuss the significant part of the code.

Through the line PrimaryStage.initStyle(StageStyle.TRANSPARENT);, we set the stage style to transparent. This will make the whole stage transparent.

Please note that we used the method initStyle() for this purpose. After compiling the above example code and running it in your environment, you will get the below output.

JavaFX Transparent Background Output

An Alternative Way to Make a Transparent Stage in JavaFX

JavaFX generates its UI by using FXML with the support CSS. You can easily build or modify your UI with Scene Builder, provided officially by Oracle.

Through this tool, you can easily add CSS properties to your UI and add many other styles to your UI by just using mouse clicks. Or, you can create an external CSS file and include that with your code like below,

scene.getStylesheets().add("YourCSS.css")

Remember, if your IDE doesn’t support the automatic inclusion of Libraries and Packages. Then you may need to manually include these necessary Libraries and Packages before compiling.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Java JavaFX