Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default font of JavaFX WebView

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

like image 652
NickL Avatar asked Jul 31 '13 12:07

NickL


People also ask

How do I change the default Font in Javafx?

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.

What is the default Font for Javafx?

There is a Segoe UI as default Font on your Windows 7 system, but the version is 5.01 per default.


3 Answers

I found an even easier way:

engine.setUserStyleSheetLocation("data:,body { font: 12px Arial; }");
like image 85
Patric Bechtel Avatar answered Oct 11 '22 07:10

Patric Bechtel


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

like image 40
SWdV Avatar answered Oct 11 '22 06:10

SWdV


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.

like image 41
jewelsea Avatar answered Oct 11 '22 07:10

jewelsea