Set Javafx. Root Styles in Css
I Want to Apply Several Styles (Font, Font-Size) to My Components Initially via Css-File. (I Am Using the Scenebuilder 2.0 from Oracle.) So I Added My Css-File...
I want to apply several styles (font, font-size) to my components initially via css-file.
(I am using the SceneBuilder 2.0 from oracle.)
So I added my css-file to the top component.
My css-File looks like this:
Application.css
.root {
-fx-font: 14px Arial;
}
.button {
-fx-font: 28px Arial;
}
My Application.fxml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="240.0" prefWidth="320.0" stylesheets="@Application.css" xmlns="" xmlns:fx="">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="MyText" />
<Label text="MyLabel" />
<Button mnemonicParsing="false" text="MyButton" />
</children>
</VBox>
now I add a Text-Element or Label-Element to my pane, the font doesn't get affected by the root node. But if I add a Button-Element to my pane, the style for .button gets applied.
I expected the .root node would affect the style of any other elements. like in this answered question but it did not work:
Set Font globally in JavaFX
can anyone maybe explain what am I doing wrong?
the only way to archive somthing similar is: setting the style directly as:
-fx-font: 14px Arial;
under Style which i wish i could store in a css file.
1 Answer
I deleted my previous answer. It was not accurate and it was a little confusing.
A container control doesn't have a property -fx-font. So when you define:
.root {
-fx-font: 14px Arial;
}
it has no effect at all.
If you want to set the font for all children controls you should do:
.root * {
-fx-font: 14px Arial;
}
or if you want to change only specific controls of a container:
.table-view .label {
-fx-font: 14px Arial;
}
or all labels of the application:
.label {
-fx-font: 14px Arial;
}