Search code examples
javajavafxintellij-14

Why is font name not changing? (Java FX)


I'm having a problem with a javafx label. No matter what font I type in the below font constructor, compile and run the IntelliJ project, it doesn't change the font displayed.

 mainLabel.setFont(new Font("Gotham",18));

Here is my JavaFx Program so far:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javafx.scene.text.Font;


public class JavaFXMainWindow extends Application {


    public static void main(String args[]) {


        launch(args);
    }


    public void init() {


    }


    //Program starts here
    public void start(Stage mainStage) {

        //Setting the stage
        mainStage.setTitle("Ping Tool");
        mainStage.setResizable(false);
        mainStage.setFullScreen(false);


        //Creating the root node for all other nodes
        FlowPane rootNode = new FlowPane();


        //Setting the main scene, adding the root node to it and passing dimensions
        Scene mainScene = new Scene(rootNode, 1000, 800);

        //placing the scene on the stage
        mainStage.setScene(mainScene);

        //Setting a label ( no matter what argument I pass to the font it still 
        //shows the same font. The size argument works fine though)

        Label mainLabel = new Label("Please enter IP addresses below:");
        mainLabel.setFont(new Font("Gotham",18));

        //Adding the node to the tree
        rootNode.getChildren().addAll(mainLabel);

        //making the Stage visible
        mainStage.show();
    }


    public void stop() {


    }


}

Solution

  • I can confidently confirm that changing the line mainLabel.setFont(new Font("Gotham",18)); does indeed change the displayed font, at least in my environment (BlueJ). Are you making sure to compile so you're using the updated .class files?