Change default font for JavaFX WebView

I am using Java FX2.2 WebView built into JPanel to display web pages. This works well, with the exception of the default font, which I don't like. It looks very bold / rounded / smooth / blurry.

enter image description here

To the left is the text from my Chrome browser, to the right of the FX browser.

It seems that the default font is "System Regular", this font is returned by Font.getDefault (). I tried changing it with reflection, but the browser still uses the same font. I also looked at WebView.setFontScale() and WebView.setFontSmoothingType() , the former has been resized, and the latter has only the second type of font smoothing, which is even worse than the default.

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

Does anyone know how to change this standard font for Java FX WebView?

EDIT: function request was requested here: https://bugs.openjdk.java.net/browse/JDK-8090968

+4
source share
3 answers

Try using jdk8 , where (I believe), the default font rendering engine for some platforms has been updated. You will need to evaluate if there are improvements on your target platforms.

Also note that there is an existing function request to make rendering fonts in explicit JavaFX format (RT-10778) .

WebView loads HTML, so it uses standard methods for changing fonts in HTML (legacy HTML font tag or CSS).

Regarding setting the default font used by WebView, I don't know about the mechanism for this. You can create a function request against a JavaFX environment project. If you register a feature request, you can refer to the Chrome documentation , which shows that this feature is available in Chrome.

+2
source

I found an even simpler way:

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

This is how I changed the default font:

I used

 engine.setUserStyleSheetLocation(getClass().getResource("style_html.css").toExternalForm()); 

and in this stylesheet I wrote:

 html { font: 17px Impact; /*for example*/ } 

This works great for me.

+1
source

Source: https://habr.com/ru/post/1494487/


All Articles