I have a Maven project in IntelliJ IDEA, using JDK 15 and JavaFX 15.
I tried:
- Set style for WebEngine
- Set blend mode for WebView
- Tried WebPage
Nothing succeeded. How do I make the background of the WebView transparent?
My module-info.java:
module project {
requires javafx.controls;
requires javafx.web;
requires javafx.graphics;
requires uk.co.caprica.vlcj;
requires uk.co.caprica.vlcj.javafx;
exports project;
}
My pom.xml dependencies:
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>15</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>15</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>15</version>
</dependency>
<dependency>
<groupId>uk.co.caprica</groupId>
<artifactId>vlcj</artifactId>
<version>4.7.0</version>
</dependency>
<dependency>
<groupId>uk.co.caprica</groupId>
<artifactId>vlcj-javafx</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
I created a new project and tried to implement the example from the answer. Happened. Unfortunately, the background color does not become transparent after clicking the button. Do you have any idea why the example didn't work?
Main.java:
import javafx.application.Application;
import javafx.stage.Stage;
import com.sun.webkit.dom.HTMLDocumentImpl;
import java.lang.reflect.Field;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.web.WebView;
public class Main extends Application {
@Override
public void start(final Stage stage) {
initStage(stage);
}
private void initStage(Stage stage){
WebView wv = new WebView();
wv.setStyle("-fx-background-color: transparent;");
wv.getEngine().load("https://w3schools.com");
VBox vb = new VBox(wv);
vb.setBackground(new Background(new BackgroundFill(Color.ORANGERED, CornerRadii.EMPTY, Insets.EMPTY)));
Button b = new Button("remove page background");
b.setOnAction((event) -> {
HTMLDocumentImpl cast = (HTMLDocumentImpl) wv.getEngine().getDocument();
try {
Field f = wv.getEngine().getClass().getDeclaredField("page");
f.setAccessible(true);
com.sun.webkit.WebPage page = (com.sun.webkit.WebPage) f.get(wv.getEngine());
page.setBackgroundColor((new java.awt.Color(1, 1, 1, 1)).getRGB());
f.setAccessible(false);
} catch (Exception e) {
}
});
vb.getChildren().add(b);
stage.setScene(new Scene(vb));
stage.show();
}
public static void main(String[] args) {
launch();
}
}
VM options:
--module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.web --add-exports javafx.web/com.sun.webkit.dom=ALL-UNNAMED