JavaFX loading a stylesheet


JavaFX has a method that is added to the controller:
public void initialize(URL url, ResourceBundle rb)

This seems to run before any of the controls are added to the scene, because when I add this to it:

public void initialize(URL url, ResourceBundle rb){
    String treeItemCss = getClass().getResource("/media/css/TreeItem.css").getPath();

The CSS:

    -fx-indent: 100;
    -fx-underline: true;

I get an error from this method: getStylesheets(). But if I move that to an OnAction and execute that action I get no errors.

So my question is, is there a method that runs after all the controls are added to the scene, or a good way to add css to items that are created from a user action, such as a button click?

11/11/2014 9:53:03 PM

Accepted Answer

The initialize() method runs at the end of the FXMLLoader's load() method. Since you don't get a reference to the root of the FXML until that completes, there's obviously no way you can add it to a scene until after then.

You can:

Add the css to the Scene in the application code. I.e. Somewhere you create an FXMLLoader, call load(), and add the result to the Scene. Just set the css file on the scene right there, or:

Add the css stylesheet to the root node instead of to the scene (assuming main is a Parent):

public void initialize() {
    String treeItemCss = ... ;


Observe the Scene property and add the stylesheet when it changes to something not null:

public void initialize() {
    String treeItemCss = ... ;
    main.sceneProperty().addListener((obs, oldScene, newScene) -> {
        if (newScene != null) {

Update Here is a complete example to demonstrate the second option. Everything is in the "application" package:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {
    public void start(Stage primaryStage) {
        try {
            BorderPane root = FXMLLoader.load(getClass().getResource("Main.fxml"));
            Scene scene = new Scene(root,400,400);
        } catch(Exception e) {

    public static void main(String[] args) {


<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.control.TreeItem?>

<BorderPane xmlns:fx="" fx:controller="application.MainController" fx:id="root">
            <TreeItem value="Root">
                    <TreeItem value="One"/>
                    <TreeItem value="Two"/>
                    <TreeItem value="Three"/>

package application;

import javafx.fxml.FXML;
import javafx.scene.layout.BorderPane;

public class MainController {
    private BorderPane root ;

    public void initialize() {


    -fx-indent: 100;
    -fx-underline: true;

Note that you can add the stylesheet directly in the FXML file with

<BorderPane xmlns:fx="..." fx:controller="..." stylesheets="@application.css">

and then omit it completely from the controller logic.

11/12/2014 2:04:01 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow