Marogo
Marogo

Reputation: 25

How to get font settings from CSS file to Font object?

I would like to get a font settings from a CSS file:

#myCustomFont
{
  -fx-font-family: "Segoe UI";
  -fx-font-size: 14px;
  -fx-font-weight: bold;
  -fx-text-fill: red;
 }

and then pass it as a parameter in several methods as Font object. For this, I used the auxiliary Text object:

Text t = new Text("Some text");
t.setId("myCustomFont");
t.applyCss();
Font myFont = t.getFont();

Unfortunately font myFont is not like in the CSS file, it has remained the default. The same problem is with setting the font in the Label object created in code similarly to the above, but if Label is implemented in fxml file of my project, font myFont2 is like in CSS:

 dateTimeLabel.setText("12.01.2022"); //Label from .fxml
 dateTimeLabel.setId("myCustomFont");
 dateTimeLabel.applyCss();
 Font myFont2 = dateTimeLabel.getFont(); //OK

What am I doing wrong?

Upvotes: 1

Views: 96

Answers (2)

Marogo
Marogo

Reputation: 25

Thank you very much for your answer. I completely forgot that a dynamically created label needs to be related in some way to the scene for which the style file is being set up: scene.getStylesheets().add(relative_path_to_css_file)

In your example You assigned a style to Label as CUSTOM_FONT_CSS String, but I need to read the style from the css file that is assigned to the scene, so I used this trick:

  Label label = new Label();
  somePane.getChildren().add(label); //some Pane located on the scene (from .fxml)
  label.setId("myCustomFont");
  label.applyCss();
  Font myFont = label.getFont();
  somePane.getChildren().remove(label); //cleaning

In this way, the Label becomes associated with the scene and the style file, which will make setting some #id contained in the css file work.

Upvotes: 0

jewelsea
jewelsea

Reputation: 159341

This works as expected for me.

The node on which CSS is being applied should be in a scene before the CSS is applied to it.

See the documentation for applyCss():

This method is a no-op if the Node is not in a Scene. The Scene does not have to be in a Stage.

The example application will output to the command line, the following value:

Font[name=Comic Sans MS Bold, family=Comic Sans MS, style=Bold, size=40.0]

Tested on OS X, JavaFX 17.0.1.

Example App

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

public class FontTemplate extends Application {
    private static final String CUSTOM_FONT_CSS = """
            data:text/css,
            #customFont {
                -fx-font-family: "Comic Sans MS";
                -fx-font-size: 40px;
                -fx-font-weight: bold;
                -fx-text-fill: red;
            }
            """;

    @Override
    public void start(Stage stage) throws Exception {
        Label label = new Label("Label");
        label.getStylesheets().add(CUSTOM_FONT_CSS);
        label.setId("customFont");

        Scene scene = new Scene(label);
        label.applyCss();

        System.out.println(label.getFont());

        stage.setScene(scene);
        stage.show();
    }

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

Upvotes: 2

Related Questions