Viktoria
Viktoria

Reputation: 543

JavaFX custom Fonts

Actually searched everywhere but any answer can't help me, sohere's the problem: I want to add a custom font to my buttons. I already tried to write a css java class and a lot of other solutions, but nothing helped my.

/code deleted/

I'd be so glad if anyone could help me!

Update: I tried this:

package view;
import ...
public class SceneStyle {

ImageView header = new ImageView("/view/images/header.jpg");
ImageView chatImg = new ImageView("/view/images/chat_win.jpg");

//Layout Style
//public String font1 = "Impact";
//public String font2 = "Comic Sans MS";
public static String color1 = "#00adf0"; 
public static String color2 = "#0076a3"; 

public void setLabelStyle(Label label) {
    label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
    Scene scene = new Scene(label);
    scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
    label.setTextFill(Color.GRAY);
}

public void setLabelStyle2(Label label){
    label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
    Scene scene = new Scene(label);
    scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
    label.setTextFill(Color.GRAY);
}

public void setLabelStyle3(Label label){
    label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
    Scene scene = new Scene(label);
    scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
    label.setTextFill(Color.RED);
}

public void setButtonStyle(Button button){
    button.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 30; -fx-font-style: normal");
    Scene scene = new Scene(button);
    scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
    button.setTextFill(Color.web(color2));
    button.setPrefWidth(190);
}

public void setBorderPaneStyle(BorderPane pane, VBox btnBox, HBox scoreBox){

    pane.setTop(header);
    pane.setLeft(btnBox);
    pane.setRight(chatImg);
    pane.setBottom(scoreBox);
    pane.getLeft().setStyle("-fx-background-image: url(\"/view/images/border_left.jpg\");");
    pane.getBottom().setStyle("-fx-background-image: url(\"/view/images/bottom.jpg\");");
    //pane.getCenter().setStyle("-fx-background-image: url(\"/view/images/center.jpg\");");
    //TODO: why can't I implement this?
}

public static String getFontColor1(){ return color1; }
public static String getFontColor2(){ return color2; }

Thanks for the detailed answers, but actually I don't want to write a start method which is overwritten. Just want to implemtent the font in my code. And yes now I'm trying with the Google Fonts. Thanks for answering my question.

Upvotes: 7

Views: 26307

Answers (3)

ItachiUchiha
ItachiUchiha

Reputation: 36792

Here is a simple example of how to use custom fonts in your JavaFX application. This example is just an edited version of the sample FontLoad application.

Output

enter image description here


Project Structure

The follow diagram is the project structure.

enter image description here


Java Class

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FontLoad extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Label label = new Label("JavaFX Application");
        Button button = new Button("My Button");
        VBox box = new VBox(15, label, button);
        box.setAlignment(Pos.CENTER);
        Scene scene = new Scene(box, 500, 300);
        scene.getStylesheets().add(getClass().getResource("/fontstyle.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

Stylesheet

@font-face {
    font-family: 'Fleftex';
    src: url('fonts/Fleftex_M.ttf');
}

.label {
    -fx-font-family: 'Fleftex';
    -fx-font-size: 20;
}

.button .text {
    -fx-font-family: 'Fleftex';
}

Update >= 8u60

Starting with this version the attribute “font-family” is ignored and you have to use the real name of the TTF.

By example: to use the font “Fleftex” contained on the file Fleftex_M.ttf You have to use this CSS file :

@font-face {
src: url(“fonts/Fleftex_M.ttf”);
}

.label {
-fx-font-family: 'Fleftex';
}

Upvotes: 16

Viktoria
Viktoria

Reputation: 543

I tried this:

    public void setLabelStyle(Label label) {
    label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
    Scene scene = new Scene(label);
    scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
    label.setTextFill(Color.GRAY);

in each method. Because I don't want write a new methode which overrides the start method just like above. (Actually thanks for this detailed answer, but it isn't what I am searching for). But also my solution doesn't work well...

Upvotes: 0

mipa
mipa

Reputation: 10640

Here is a little example showing how to load and set a custom font.

package createfont;

import java.io.IOException;
import java.io.InputStream;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class CustomFontTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        String currentFontFile = "English Gothic, 17th c..TTF";
        InputStream fontStream = CustomFontTest.class.getResourceAsStream(currentFontFile);
        if (fontStream != null) {
            Font bgFont = Font.loadFont(fontStream, 36);
            fontStream.close();

            final Button button = new Button("Press me");
            button.setFont(bgFont);

            BorderPane root = new BorderPane();
            root.setCenter(button);

            Scene scene = new Scene(root, 500, 100);

            primaryStage.setTitle("CustomFontTest");
            primaryStage.setScene(scene);
            primaryStage.show();
        } else {
            throw new IOException("Could not create font: " + currentFontFile);
        }
    }

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

}

Upvotes: 3

Related Questions