Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JavaFX WebView usage, transparent background, jdk8 vs jdk9

Wes HinsleyJan 5 2018 — edited Jan 5 2018

Hi everyone - I also reported on this https://stackoverflow.com/questions/48102577/javafx-webview-usage-transparent-background-jdk8-vs-jdk9  but on reflection, here might have been a better place to post it. I want to write code that uses JavaFX WebView - and ideally I want to keep the same codebase for compiling with JDK8 and JDK9, but I'm running into some issues.

The following code is intended to show some white text on a black background, in a WebView, where the background of the webpage is transparent. (ie, the text can be layered on top of a movie or an image in the stackpane. This method works well on JDK8 - the code below is a minimal example from a larger application).

import java.lang.reflect.Field;
import org.w3c.dom.Document;
import com.sun.webkit.WebPage;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class TestWebView extends Application {

  public void start(Stage stage) throws Exception {
   
StackPane stackpane = new StackPane();
   
Scene scene = new Scene(stackpane, stage.getWidth(), stage.getHeight(), Color.BLACK);
    stage
.setScene(scene);
    scene
.setFill(Color.BLACK);
    stackpane
.setStyle("-fx-background-color: BLACK");
   
WebView webview = new WebView();
    stackpane
.getChildren().add(webview);
   
WebEngine webengine = webview.getEngine();
    webengine
.documentProperty().addListener(new WebDocumentListener(webengine));
    webengine
.loadContent("<p style='color:white'>Hello World</p>");
    stage
.show();
 
}

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

 
protected class WebDocumentListener implements ChangeListener<Document> {
   
private final WebEngine wdl_webEngine;

   
public WebDocumentListener(WebEngine webEngine) {
      wdl_webEngine
= webEngine;
   
}

   
@Override
    public void changed(ObservableValue<? extends Document> arg0, Document arg1, Document arg2) {
     
try {
       
Field f = wdl_webEngine.getClass().getDeclaredField("page");
        f
.setAccessible(true);
        com
.sun.webkit.WebPage page = (WebPage) f.get(wdl_webEngine);
        page
.setBackgroundColor((new java.awt.Color(0, 0, 0, 0)).getRGB());
     
} catch (Exception e) {
        e
.printStackTrace();
     
}
   
}
 
}
}

Testing on MacOS 10.11.6, with Oracle's JDK:

With JDK 1.8.0_152, this code works nicely - I get white text on black. (And the transparency works too when I layer things underneath it in the stackpane)

With JDK 9 (9+181), I have to compile a different way (which is not a problem) because com.sun.webkit.WebPage is no longer accessible. I compile and run it with --add-exports javafx.web/com.sun.webkit=ALL-UNNAMED - but having done that, I get black text on a black webpage. I can tell the text is there by selecting the text and dragging it, which makes the text appear white while it's being dragged.

Can anyone point me in the right direction - ideally, I'd like to write one piece of code that does the same thing in JDK8 as JDK9. But as a second best, any pointers as to why it doesn't work in JDK9 would be really helpful. The code is taken from a much larger JavaFX app - and it looks like migrating to JDK9 is going to be tricky...

Many thanks for any ideas,
Wes

Message was edited by: Wes Hinsley - fixed some typos and made the start a bit clearer

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 2 2018
Added on Jan 5 2018
1 comment
1,213 views