Applying small caps to JavaFX Text objects

315 Views Asked by At

I'm trying to make a Text node small caps. In CSS, I can do font-variant: small-caps; to make small caps text; however, JavaFX does not seem to have this. I know I can use a TextFlow with two font sizes, but this seems like an inelegant solution.

Is there any simple way to make text small caps in JavaFX?

1

There are 1 best solutions below

2
SedJ601 On

Google "Google fonts". Also, google to see if you can find some fonts that are naturally small caps. I found one. You can see how to use Google fonts in a JavaFX app below.

code from here.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

/**
 *
 * @author Sedrick
 */
public class JavaFXApplication44 extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("Hello @FontFace\n-- Gafata --");
        label.setStyle("-fx-font-family: 'IM Fell French Canon SC', serif; -fx-font-size: 80;");
        Scene scene = new Scene(label);
        scene.getStylesheets().add("https://fonts.googleapis.com/css?family=IM+Fell+French+Canon+SC");
        primaryStage.setTitle("Hello @FontFace");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

enter image description here