I've noticed that the JavaFX components I use, unlike their Swing counterparts, are not rendering some system fonts properly. Here's the complete example that presents the issue.
public class JavaFXApplication1 extends Application {
@Override
public void start(Stage primaryStage) {
// Swing button
SwingNode node = new SwingNode();
SwingUtilities.invokeLater(() -> {
JButton swingButton = new JButton("Comic Sans MS");
swingButton.setFont(new java.awt.Font("Comic Sans MS", java.awt.Font.PLAIN, 20));
node.setContent(swingButton);
});
// JavaFX button
Button fxButton = new Button("Comic Sans MS");
fxButton.setFont(Font.font("Comic Sans MS", 20));
HBox hbox = new HBox();
hbox.getChildren().addAll(node, fxButton);
Scene scene = new Scene(hbox, 300, 250);
primaryStage.setTitle("Font test");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If you have the Comic Sans MS font in your system, you should see the following result. Only the Swing component looks fine:
Apparently the FX graphics system does not load all system fonts on startup. The most obvious solution is to load the missing ones manually, like that:
Font.loadFont(new FileInputStream(System.getenv("WINDIR")+"\\fonts\\"+"comic.ttf"), 20);
Now everything renders properly, but this solution isn't cross platform and I'd really expect it to work out-of-the-box just like it was in Swing. Am I missing something? If not, then why only a subset of system fonts is loaded into the JavaFX environment?
I'm running it on Windows 10.