JavaFX provides styling by CSS. CSS support is based on the W3C CSS version 2.1, but there are some minor differences that can be found in the JavaFX CSS documentation (http://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html).
In JavaFX, a stylesheet can be applied to the scene graph or to a specific node. A stylesheet that is applied to the scene graph will affect all nodes in the scene graph. If the stylesheet is applied to a node, it will affect this node and all its children (recursively). Several stylesheets can be applied to the scene graph or a node. The following code snippet shows how you can set a stylesheet for a scene graph or a node:
// load the stylesheet
String style = getClass().getResource("style.css").toExternalForm();
// apply stylesheet to the scene graph
myScene.getStylesheets().addAll(style);
// apply stylesheet to a node
parentPanel.getStylesheets().addAll(style);
JavaFX also supports inline stylesheets for nodes. Here you can define the CSS rules directly in your Java code as a string, which can be helpful for debuging and testing. The Node class provides a method that can be used to set an inline style for a Node instance:
button.setStyle("-fx-background-color: green;");
A CSS rule is applied to a node if its selector matches. In the selector, one can use a combination of ID, element classes, style classes, and pseudo classes. A JavaFX node can have exactly one ID:
mySaveButton.setId(„my-save-button“);
Here is an example of a CSS rule that styles exactly this button:
/* The # sign in the selector defines an id */
#my-save-button {
-fx-background-color: blue;
}
In addition to the ID, a node can have several style classes:
button.getStyleClass().add(“toolbar-button“);
All nodes that have the "toolbar-button" style class can be styled with a single rule in CSS:
/* The . sign in the selector defines a style class */
.toolbar-button {
-fx-background-color: blue;
}
Pseudo classes can be defined for any node, too. Pseudo classes are activated and deactivated by using the Java API:
// Define the pseudo class
PseudoClass myPseudoClass = PseudoClass.getPseudoClass("active");
//activate the pseudo class
myNode.pseudoClassStateChanged(myPseudoClass, true);
//deactivate the pseudo class
myNode.pseudoClassStateChanged(myPseudoClass, false);
In CSS you can use the pseudo class in a selector to define a specific style:
/* A colon signals a pseudo class */
.control:active {
-fx-background-color: blue;
}
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}