Baspro
Baspro

Reputation: 35

How can I change the size of 1 specific label's font if I have a CSS file for labels?

I have a CSS file with:

label {
-fx-font-size: 10px;
-fx-font-weight: bold;
-fx-text-fill: #333333;
-fx-effect: dropshadow( gaussian , rgba(255,255,255,0.5) , 0,0,0,1 );
}

If I want to have one label with another style, how can I do it?I'm trying it with the following code, but it does not change.

l_mov.setFont(Font.font("calibri", FontWeight.BOLD, FontPosture.REGULAR, 
25));

Upvotes: 0

Views: 442

Answers (2)

Pagbo
Pagbo

Reputation: 690

Even if the Chao-Wen chen solution seems to be the best one for your case : (using the id). You should keep in mind that you also can use your own css styleclass. Here is an example to be more exhaustive :

public class CssApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        VBox vb = new VBox(10.0);

        // Title
        Label title = createTitle("Title");

        // SubTitle 1
        Label subtitle1 = createSubTitle("SubTitle1");
        //Standard text
        Label lorem = new Label("Lorem ipsum dolor sit amet, mea eu nibh sonet accusam, mea dicunt oblique et. Mei purto efficiantur ne, quas audiam consulatu at eum. Ea sit dicta zril, adipisci praesent pertinacia ei his.");

        // SubTitle 2
        Label subtitle2 = createSubTitle("SubTitle2");
        //Standard text
        Label ipsum = new Label("Amet dictas consequat ut vix, maluisset hendrerit vim ex, ne pro tale aliquid accusata. Mea porro aperiri voluptua te, case lorem per eu.");
        Label specificText = new Label("I am a specific text with a particular styling.");
        specificText.setId("specific");

        vb.getChildren().addAll(title, subtitle1, lorem, subtitle2, ipsum, specificText);
        Scene scene = new Scene(vb, 500.0, 400.0);
        scene.getStylesheets().add(this.getClass().getResource("style.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
    }


    private static Label createTitle(String pText) {
        Label newLabel = new Label(pText);
        newLabel.getStyleClass().add("label-title");
        return newLabel;
    }

    private static Label createSubTitle(String pText) {
        Label newLabel = new Label(pText);
        newLabel.getStyleClass().add("label-subtitle");
        return newLabel;
    }
}

And here is the css stylesheet. Please note that the order of your styleclass is important.

.label {
    -fx-font-family: serif;
    -fx-font-size: 12.0;
    -fx-font-weight: normal;
    -fx-text-fill: black;
}

.label-title {
    -fx-font-family: sans-serif;
    -fx-font-size: 50.0;
    -fx-font-weight: bold;
    -fx-text-fill: red;
}

.label-subtitle {
    -fx-font-family: monospace;
    -fx-font-size: 19.0;
    -fx-font-style: oblique;
    -fx-font-weight: normal;
    -fx-text-fill: blue;
}

#specific {
    -fx-text-fill: green;
}

That deals with this kind of example (this a simple one only for demonstration) :

enter image description here

For this example I only use one stylesheet, but a different stylesheet could be applied for a specific panel (this one will override all the ancestor ones if same style class are provided).

Upvotes: 3

ChaoWen
ChaoWen

Reputation: 21

If you have two labels with different styles, maybe you can use two different 'id' to identify them.

Upvotes: 2

Related Questions