Defining StylesA style definition consists of the name of the style, also called the selector, and a series of rules that set the properties for the style. Rules for a definition are enclosed in braces ({}).
Example 2 shows the definition for a style named .custom-button.
Example 2 Sample Style Definition
.custom-button { -fx-font: 16px "Serif"; -fx-padding: 10; -fx-background-color: #CCFF99;}
Note: The size of a font can be specified in either points (pt) or pixels (px). A resolution of 96 dots per inch (dpi) is assumed, so 1px = 0.75pt. |
SelectorsSeveral types of styles can be defined. Each type of style has its own convention for selectors.
Style classes correspond to class names. By convention, style class names that consist of more than one word use a hyphen (-) between words. Style class selectors are preceded by a dot (.).
Examples of class selectors:
.button.check-box.scroll-barYou can also define styles that are associated with a node through the node's ID. The ID is set using the node's setId() method. The style name is the ID preceded by a hash symbol (#). For example, a node with the ID my-button is skinned with the style #my-button.
Examples of ID style selectors:
#my-button#shaded-hboxCompound selectors are also possible. Some classes include elements that can have their own style definition, which are called descendant classes. For example, many UI controls have a descendant class for the label. These definitions are identified by the selector for the class and the selector for the descendant class separated by a space.
Examples of selectors for descendant classes:
.check-box .label.check-box .box.radio-button .dotPseudo-classes enable you to customize states of a node, such as when a node has focus. These definitions are identified by the selector for the class and the name for the state separated by a colon (:).
Examples of selectors for pseudo-classes:
.radio-button:focused.radio-button:hover.scroll-bar:vertical
Rules and PropertiesThe rules for a style definition assign values to properties associated with the class. Rule property names correspond to the names of the properties for a class. The convention for property names with multiple words is to separate the words with a hyphen (-). Property names for styles in JavaFX style sheets are preceded by -fx-. Property names and values are separated by a colon (:). Rules are terminated with a semicolon (;).
Examples of rules:
-fx-background-color: #333333;-fx-text-fill: white;-fx-alignment: CENTER;The .root style class is applied to the root node of the Scene instance. Because all nodes in the scene graph are a descendant of the root node, styles in the .root style class can be applied to any node.
The .root style class includes properties that can be used by other styles to provide consistency in a UI. For example, the property -fx-focused-base is defined in the .root style. This property is used by styles for other UI controls as the color for the control when it has focus. The following definition shows how this property is used in the style for the classCheckBox:
.check-box:focused { -fx-color: -fx-focused-base;}