Change default font of JavaFX WebView


Question

I am using a Java FX2.2 WebView embedded in a JPanel for showing web pages. This works well, except for the default font that I don't like. It looks very bold/rounded/anti-aliased/blurry.

enter image description here

On the left is the text from my Chrome browser, on the right of the FX Browser.

It seems that it is using the "System Regular" Font as a default font, this font is returned by Font.getDefault(). I have tried changing it with reflection but the browser still uses the same font. Also I looked into WebView.setFontScale() and WebView.setFontSmoothingType(), the first only changes the size and the latter only has a 2nd font smoothing type that is even worse than the default.

I looked at Safari, which is, like FX WebView, based on WebKit, and this browser has the option to change the default font (I think most browsers have this option).

Does anyone know a way to change this default Font for the Java FX WebView?

EDIT: Made a feature request here: https://bugs.openjdk.java.net/browse/JDK-8090968

1
5
3/18/2017 12:05:57 PM

Accepted Answer

Try using jdk8 where (I believe), the default font rendering mechanism for some platforms has been updated. You will need to evaluate yourself whether there appears to be an improvement to your eyes on your target platforms.

Also note there is an existing feature request to make the rendering fonts in JavaFX clearer (RT-10778).

WebView loads HTML, so the standard methods of changing fonts in HTML apply to it (the deprecated HTML font tag or CSS).

As to setting the default font used by WebView - I am unaware of a mechanism to do that. You can create a feature request against the JavaFX runtime project. If you log a feature request, you may want to reference the Chrome documentation which demonstrates that this feature is available in Chrome.

2
3/11/2016 12:08:12 PM

I found an even easier way:

engine.setUserStyleSheetLocation("data:,body { font: 12px Arial; }");

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon