11

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");

        Button btn = new Button();
        btn.setText("Hello");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

Getting following first lines on console:

35026:1978749] CoreText note: Client requested name ".SFNS-Regular", it will get Times-Roman rather than the intended font. All system UI font access should be through proper APIs such as CTFontCreateUIFontForLanguage() or +[NSFont systemFontOfSize:]. 2021-06-09 00:00:46.808 java[35026:1978749] CoreText note: Set a breakpoint on CTFontLogSystemFontNameRequest to debug. 2021-06-09 00:00:46.815 java[35026:1978749] CoreText note: Client requested name ".SFNS-Regular", it will get Times-Roman rather than the intended font. All system UI font access should be through proper APIs such as CTFontCreateUIFontForLanguage() or +[NSFont systemFontOfSize:]. 2021-06-09 00:00:46.982 java[35026:1978817] CoreText note: Client requested name ".SFNS-Regular", it will get Times-Roman rather than the intended font. All system UI font access should be through proper APIs such as CTFontCreateUIFontForLanguage() or +[NSFont systemFontOfSize:].

OS: macOS javafx-sdk-11.0.2 jdk 11

instead of Hello getting following text on button enter image description here

RyuzakiSultan
  • 123
  • 1
  • 7
  • I don't have an answer, just a suggestion. By default, JavaFX uses logical fonts which have mappings to physical fonts and those mappings also depend on the default [locale](https://www.oracle.com/java/technologies/javase/jdk8-jre8-suported-locales.html). I don't have MacOS but you should check what physical font Java is trying to load and then check whether that font can display `Hello World!`. – Abra Jun 09 '21 at 07:59

5 Answers5

15

The issue is that you do not have the font: ".SFNS-Regular" either installed or configured correctly so that JavaFx can tell where it is. I'm actually not sure if it is because of the font not being installed/configured correctly but....

What I had to do to resolve this was to add:

* {
   -fx-font-family: 'serif'
}

in my root CSS file. If you are not using CSS files or are using inline styles, then you can still set a global font:

Instead of this:

primaryStage.setScene(new Scene(root, 300, 250));

You can do something like this:

Scene scene = new Scene(root, 300, 250);
scene.getRoot().setStyle("-fx-font-family: 'serif'");
primaryStage.setScene(scene);

You can read more about JavaFX fonts here as well as additional CSS configurations for the components. Using CSS files is a great way to separate the component styles from their logic and if you have created web pages before, it will feel very familiar and really easy to pick up.

Duhvoi
  • 151
  • 5
0

I fixed the issue by switching the Module SDK back to 11.0.8. You can find that here on the Java SE 11 Archive Downloads page.

Seems that this is isolated to 11.0.12.

  • 1
    Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 07 '21 at 18:11
0

I was able to resolve the issue by adjusting the JDK version and I was also using macOS and JDK 11. Although I did not have JDK 8, JDK 14 worked fine and JDK 17 did not. It will work fine if you change the project structure to a more compatible JDK version with JavaFx.

Sakhawat
  • 31
  • 4
0

Using another version of JDK11 fixed this for me:

JDK 11.0.15, AdoptOpenJDK(HotSpot)

Lynne
  • 454
  • 7
  • 16
-5

How can one correct the same issue in a "fxml."

<font>
    <Font name="Times New Roman bold" size="20"/>
    </font>

in other words, What would replaced in <Font...."20"/> to make the same correction. Thanks kinda new at this

  • Is that a question or the answer? – Marcin Rzepecki Sep 02 '21 at 17:21
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 02 '21 at 17:21
  • I'm guessing your answer is a question and the answer to your question is: [FXML Label text bold](https://stackoverflow.com/questions/49813907/fxml-label-text-bold) – jewelsea Sep 02 '21 at 23:24
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. – Procrastinator Sep 03 '21 at 05:46