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.
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
You can change the font size and color of the text using the setFont() method. This method accepts an object of the Font class. The class named Font of the package javafx.
There is a Segoe UI as default Font on your Windows 7 system, but the version is 5.01 per default.
I found an even easier way:
engine.setUserStyleSheetLocation("data:,body { font: 12px Arial; }");
This is how I changed the default font:
I used
engine.setUserStyleSheetLocation(getClass().getResource("style_html.css").toExternalForm());
and in that stylesheet I wrote:
html {
font: 17px Impact; /*for example*/
}
It works fine for me
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With