0

I have to create a GUI using JavaFX on a Windowns PC ; when running the program, the letters print out completely weird, almost as if it is a different language.

I tried to type in 001 into one of the text fields, but it typed 223. Example of funky looking text:

Example of funky looking text

The only reason that the labels look normal is because I have specified that its font must be Verdana. Also, when printing text to the console, it prints out correctly; even when printing, the the button's text to the console it is correct.

Here is the code:

package com.itjva2.question3;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;


/**
 * JavaFX App
 */
public class App extends Application {

    @Override
    public void start(Stage primaryStage) {
        
        TextField txtPatientID = new TextField();
        TextField txtName = new TextField();
        TextField txtGender = new TextField();
        TextField txtWeight = new TextField();
        TextField txtHeight = new TextField();
        TextField txtMessage = new TextField();
        RadioButton rbMale = new RadioButton("Male");
        RadioButton rbFemale = new RadioButton("Female");
        
 //Variables for font height width color & more
        double dHeight = 40;
        double dWidth = 250;

//      Create 6 labels and 6 corresponding text boxes
        //Labels
        Label lblPatientID = new Label("Patient_ID");
        double FontSize = 13;
        lblPatientID.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblName = new Label("Name");
        lblName.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblGender = new Label("Gender");
        lblGender.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblWeight = new Label("Weight in Kg");
        lblWeight.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblHeight = new Label("Height in metres");
        lblHeight.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        Label lblMessage = new Label("Message");
        lblMessage.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));

        //RadioButton
        TilePane r = new TilePane();
        rbMale.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        rbFemale.setFont(Font.font("Verdana", FontWeight.BOLD, FontSize));
        r.getChildren().add(rbMale);
        r.getChildren().add(rbFemale);
        r.setHgap(195);
        r.setPrefTileHeight(40);
        r.relocate(0, 40*3);

        //Buttons
        Button btnCalculate = new Button("Calculate BMI");
        Button btnDisplay = new Button("Display");

//      Position all elements on the screen correctly
        lblPatientID.relocate(5, 15);
        txtPatientID.relocate(250, 0);
        txtPatientID.setPrefWidth(dWidth);
        txtPatientID.setPrefHeight(dHeight);
        lblName.relocate(5, 55);
        txtName.relocate(250, 40);
        txtName.setPrefWidth(dWidth);
        txtName.setPrefHeight(dHeight);
        lblGender.relocate(5, 95);
        txtGender.relocate(250, 40*2);
        txtGender.setPrefWidth(dWidth);
        txtGender.setPrefHeight(dHeight);
        r.setStyle("-fx-background-color: #D6D5CB;");
        lblWeight.relocate(5, 175);
        txtWeight.relocate(250, 40*4);
        txtWeight.setPrefWidth(dWidth);
        txtWeight.setPrefHeight(dHeight);
        lblHeight.relocate(5, 215);
        txtHeight.relocate(250, 40*5);
        txtHeight.setPrefWidth(dWidth);
        txtHeight.setPrefHeight(dHeight);
        lblMessage.relocate(5, 255);
        txtMessage.relocate(250, 40*6);
        txtMessage.setPrefWidth(dWidth);
        txtMessage.setPrefHeight(dHeight);
        btnCalculate.relocate(0, 40*7);
        btnCalculate.setPrefWidth(dWidth);
        btnCalculate.setPrefHeight(dHeight);
        btnDisplay.relocate(250, 40*7);
        btnDisplay.setPrefWidth(dWidth);
        btnDisplay.setPrefHeight(dHeight);



//      Group all elements together and add to the scene
        Group grpAll = new Group(lblPatientID,lblName,lblGender,lblWeight,lblHeight,r,lblMessage,txtPatientID,txtName,txtGender,txtWeight,txtHeight,txtMessage,btnCalculate,btnDisplay);

    //  FIX BUG Code
//        grpAll.setStyle(chinese_fix);


        Scene sceneMain = new Scene(grpAll, 500, 365);
        sceneMain.setFill(Color.YELLOW);

//      Set height & title of the APP-Window
        primaryStage.setTitle("GWC Timer App");
        primaryStage.setWidth(500);
        primaryStage.setHeight(365);

//      Set the scene to the stage and show the stage
        primaryStage.setScene(sceneMain);
        primaryStage.show();

        System.out.println(btnDisplay.getText());
    }

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

}
  • 1
    The code works perfectly fine in my test environment with JavaFX 11 and JDK 15. Can you provide any more details on your JavaFX version and JDK version, and is there anything else you can add? What happens if you create a brand new project with this code? Dose the code work differently if compiled and run from command line rather than an IDE? Is the default language on your computer set to something odd? Is your IDE or code editor set to an odd encoding? Are you using an experimental JavaFX library? – sorifiend Jun 23 '22 at 04:10
  • 2
    I advise using a recent JavaFX release. Some older versions had font rendering problems, especially on Mac, as [can be seen here](https://youtrack.jetbrains.com/issue/IDEA-266524/JavaFX-Scene-Builder-on-IntelliJ-labels-are-unrecognizable-on-macOS#focus=Comments-27-4834212.0-0) and on other questions about garbled fonts on StackOverflow. Some of the fixes were backported to later releases of earlier versions. I still think it is better to use a recent release, e.g. 18.0.1. Even if you don't end up using the later version at least you know if it is a bug that has been fixed. – jewelsea Jun 23 '22 at 06:00
  • 1
    Some [similar questions](https://stackoverflow.com/questions/66747171/why-javafx-application-and-scene-builder-is-showing-garbled-text), advise re-installing a font or checking character encoding, but I think it is more likely that it is a bug in JavaFX rendering on your os/font/javafx version combo, and, hopefully, upgrading the JavaFX version will fix it. – jewelsea Jun 23 '22 at 06:06
  • 1
    The garbled text is the buttons looks like a platform font rendering bug, but the wrong numbers is the field is quite weird, it doesn't seem likely that the rendering bug would come out with wrong numbers, but I guess the highly improbable is still possible. – jewelsea Jun 23 '22 at 06:08
  • @sorifiend So i am currently runnin ``` java version "18" 2022-03-22 Java(TM) SE Runtime Environment (build 18+36-2087) Java HotSpot(TM) 64-Bit Server VM (build 18+36-2087, mixed mode, sharing) ``` and the latest version of JavaFX. I tried running the code on netbeans and got the exact same problem. at this point i dont know what else to do... – Jacques van den Berg Jun 23 '22 at 06:55
  • 1
    I am unable to reproduce this on MacOS with Java/FX 17 LTS. You might [edit] your question to indicate host platform details and check your language settings and font availability. – trashgod Jun 23 '22 at 17:03
  • I have tried everything i can think of by now... Reinstalling of all system fonts and everything but something still is wrong and I have exams in 3 days and dont know what to do except a fresh install of windows – Jacques van den Berg Jul 03 '22 at 20:36

0 Answers0