In my application I am loading additional fonts from code, before showing Stage
. I followed this solution and my fonts are in application jar and loaded before any component need them. Problem is when I create distribution jar. Either starting by java -jar
or simply double click on jar, loading fonts takes about 10 seconds and then Stage
shows. From user point it looks like app is not starting. Running application from IDE does not produce delay, app start immediately. My code is simple:
@Override
public void start(Stage primaryStage) throws Exception {
final Font fontRoboto = Font.loadFont(Main.class.getResource("/path/to/font/Roboto-Regular.ttf").toExternalForm(), 12);
final Font fontAwesome = Font.loadFont(Main.class.getResource("/path/to/font/fontawesome-webfont.ttf").toExternalForm(), 12);
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/path/to/my/view/View.fxml"));
final Parent root = loader.load();
Scene scene = new Scene(root, 1200, 800);
primaryStage.setScene(scene);
primaryStage.show();
}
I created a simple workaround, it just first shows ProgressIndicator
, loads fonts in background Task
and on success replaces root
in Scene
:
ProgressIndicator pi = new ProgressIndicator(-1);
pi.setMaxSize(200, 200);
StackPane sp = new StackPane(pi);
Scene scene = new Scene(sp, 1200, 800);
Task fontTask = new Task<Void>() {
@Override
protected Void call() throws Exception {
final Font fontRoboto = Font.loadFont(Main.class.getResource("/path/to/font/Roboto-Regular.ttf").toExternalForm(), 12);
final Font fontAwesome = Font.loadFont(Main.class.getResource("/path/to/font/fontawesome-webfont.ttf").toExternalForm(), 12);
return null;
}
};
fontTask.setOnSucceeded(e -> {
scene.setRoot(root);
});
new Thread(fontTask, "FONT TASK").start();
primaryStage.setScene(scene);
primaryStage.show();
But this is only a workaround and showing ProgressIndicator
always on start of application, just for loading fonts is unefficient.
Creating separate Task
for loading fonts gives me another question. Do I have to call Font.loadFont
in JavaFX Application Thread? From my code snippet I assume that fonts does not have to be loaded in FX thread. All fonts shows in application correctly.
My OS: Windows 7 Home Premium SP1
Java: Java 1.8.0_101-b13
IDE: NetBeans 8.1
Has anybody encountered this issue?