-1

[Output] output

Things I have tried:

  • changing the fonts of system
  • upgrading javafx version to 20(current)
  • checked language/region setting

Here are all the files (added primary and secondary fxml files):

###################################################################

[App.java]

// I have imported all the necessary library

public class App extends Application {

    private static Scene scene;

    @Override
    public void start(Stage stage) throws IOException {
        scene = new Scene(loadFXML("primary"), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    static void setRoot(String fxml) throws IOException {
        scene.setRoot(loadFXML(fxml));
    }

    private static Parent loadFXML(String fxml) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
        return fxmlLoader.load();
    }

    public static void main(String[] args) {
        launch();
    }
}

[PrimaryController.java]

public class PrimaryController {

    @FXML
    private void switchToSecondary() throws IOException {
        App.setRoot("secondary");
    }
}

[SecondaryController.java]

public class SecondaryController {

    @FXML
    private void switchToPrimary() throws IOException {
        App.setRoot("primary");
    }
}

primary.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>

<VBox alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testing.PrimaryController">
   <children>
      <Label text="Primary View" />
      <Button fx:id="primaryButton" text="Switch to Secondary View" onAction="#switchToSecondary"/>
   </children>
   <padding>
      <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
   </padding>
</VBox>

secondary.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>

<VBox alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testing.SecondaryController">
    <children>
        <Label text="Secondary View" />
        <Button fx:id="secondaryButton" text="Switch to Primary View" onAction="#switchToPrimary" />
    </children>
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
    </padding>
</VBox>

  • 2
    Also check your language/region setting. – trashgod Apr 14 '23 at 16:32
  • It could be [this issue](https://stackoverflow.com/questions/66747171/why-javafx-application-and-scene-builder-is-showing-garbled-text) which was related to buggy font processing on some earlier JavaFX versions, such as 11 (I think on Macs). I recommend following [the suggestion to upgrade your JavaFX to the latest available stable version](https://stackoverflow.com/a/70674092/1155209) (currently 20). – jewelsea Apr 15 '23 at 13:43
  • 2
    [Edit] your question and post contents of files `primary.fxml` and `secondary.fxml`. Also post contents of any CSS files that are part of your application. – Abra Apr 16 '23 at 05:47
  • 1
    Download Idea, and create a [new JavaFX project](https://www.jetbrains.com/help/idea/javafx.html). Does it display correctly? Or does it have a similar rendering issue? – jewelsea Apr 16 '23 at 14:43
  • Does creating a simple example based on this [docs](https://openjfx.io/openjfx-docs/) display fonts properly? – JialeDu Apr 17 '23 at 05:49
  • @Abra I just added the primary and secondary fxml files – Sujan Niroula May 02 '23 at 16:21
  • @jewelsea thank you! Intellij idea editor works perfectly. I think it's just vscode that's maybe not working properly with M1 macs. – Sujan Niroula May 02 '23 at 16:34
  • @JialeDu no, it looks like it's just vscode that's having a problem with my m1 Mac. – Sujan Niroula May 02 '23 at 16:36

1 Answers1

0

Well, this is not an exact solution to this problem, but it's an alternative way to make Javafx run on M1 Macs with this problem.

After carefully examining this issue, it appears that VSCode is responsible for this error. I am not sure of the details of how it's happening, but when I try it on the Intellij Idea code editor, it works perfectly.

Solution: Use any other code editor. For me, IntelliJ Idea works the best.