PunkKDB
PunkKDB

Reputation: 95

How can I get the fonts family and font styles in javafx?

I can't seem to get just the font family without getting a bunch of font styles. Here is my code I am thinking of separating them using a .contains to check for light, bold, italic but that isn't very efficient.

for(String font : Font.getFontNames()){
            if(font.toLowerCase().contains("light") || font.contains("extrabold") || font.contains("extralight") || font.contains("light")){

            }

        fontsList.add(font);
        }

My display is this : Times New Roman

Times New Roman Bold

Times New Roman Bold Italic

Times New Roman Italic

But I only want to display the Time New Roman so only the font family.

Upvotes: 4

Views: 3258

Answers (1)

Jacob G.
Jacob G.

Reputation: 29680

The javafx.scene.text.Font class offers Font#getFamilies which, as you may guess, returns all of font families listed on the user's system.

Calling the following snippet produces the following output below it:

Font.getFamilies().forEach(System.out::println);

...

Times New Roman

...

Note: Fonts such as Copperplate Gothic Bold still exist, as that's considered to be a font family.

Upvotes: 3

Related Questions