Search code examples
cssjavafxpojo

Lookup style of POJO object in CSS


A helper Label that doesn't really fit in fxml, so it is created (as a POJO) and attached at initialize() step.

This is straightforward in fxml:

stylesheet.css: .my-label { -fx-font-size: 12px; }

my_view.fxml: <Label fx:id="label" styleClass="my-label" />

Or in POJO locally: label.setStyle("-fx-font-size: 12px;");

What is the right way to define Label in POJO, but move style to css?


Solution

  • All you need to do is add the style class:

    label.getStyleClass().add("my-label");
    

    and then use the same CSS you have for your FXML example.