Exercise #6
JavaFX uses CSS (Cascading Style Sheets) to separate visual design from application logic, similar to web development. JavaFX CSS is based on the CSS 2.1 specification with some elements from CSS 3.
1. JavaFX CSS Specifics:
-
Prefix -fx-: All JavaFX-specific properties start with the -fx- prefix (e.g.,
-fx-background-colorinstead ofbackground-color). -
Files: External files with the
.cssextension are typically used. -
Default Style: By default, JavaFX applications use the Modena theme.
-
List of Styles - A detailed overview of CSS styles in JavaFX.
1.2. Selectors:
-
Type Selector: Applied to all elements of a given type (e.g.,
.buttonfor all buttons). -
ID Selector: Applied to a specific graphical element via its ID (defined with
setId()in Java orfx:idin FXML). In CSS, the#symbol is used (e.g.,#login-button). -
Style Class: Allows applying a style to a group of elements. In CSS, the
.symbol is used (e.g.,.main-label).
3. Style Application Levels (by priority):
-
Inline styles (directly in the code): The method
setStyle("-fx-property: value;")is used. (Highest priority). -
External CSS (external file): Added to the scene graph or a specific container:
scene.getStylesheets().add("style.css");. -
User Agent Stylesheet: The system theme (Modena).
4. Pseudo-classes: JavaFX supports states such as :hover (when hovering with the mouse), :pressed (when clicked), and :focused (when the element has focus).